其他进程如何通过c++api读取shm(shared memory)数据

通过createIPCInMemoryTable创建的共享内存表,其他进程如何读取其数据,比如通过c++api如何读取

请先 登录 后评论

1 个回答

Draco Chen

server 端通过 GUI 创建一个跨进程内存表,并向该表实时写入数据

//创建流表
share streamTable(10000:0,`timestamp`temperature, [TIMESTAMP,DOUBLE]) as pubTable;
//创建跨进程共享内存表
share createIPCInMemoryTable(1000000, "pubTable", `timestamp`temperature, [TIMESTAMP, DOUBLE]) as shm_test;
//自定义订阅处理函数
def shm_append(msg) {
    shm_test.append!(msg)
}
//订阅流表pubTable的数据写入跨进程内存表
topic2 = subscribeTable(tableName="pubTable", actionName="act3", offset=0, handler=shm_append, msgAsTable=true)


C++ API 端

string tableName = "pubTable";
//构造对象
IPCInMemoryStreamClient memTable;

//创建一个存储数据的 table,要求和 createIPCInMemoryTable 中列的类型和名称一一对应
vector<string> colNames = {"timestamp", "temperature"};
vector<DATA_TYPE> colTypes = {DT_TIMESTAMP, DT_DOUBLE};
int rowNum = 0, indexCapacity=10000;
TableSP outputTable = Util::createTable(colNames, colTypes, rowNum, indexCapacity); // 创建一个和共享内存表结构相同的表

//overwrite 是否覆盖前面旧的数据
bool overwrite = true;
ThreadSP thread = memTable.subscribe(tableName, print, outputTable, overwrite);

//传入 subscribe 中处理数据的回调函数
void print(TableSP table) {
    //处理收到的数据
}

//最后取消订阅,结束回调
memTable.unsubscribe(tableName);

更多c++ api相关教程请参考https://gitee.com/dolphindb/api-cplusplus/tree/release200

请先 登录 后评论