使用c++API的threadedClient.subscribe订阅流数据,为什么订阅程序直接崩溃了,没有订阅成功

server:1.30.17

c++api:release130

复现代码:

#include <iostream>
#include <thread>
#include <assert.h>
#include "DolphinDB.h"
#include "Util.h"
#include "Streaming.h"
#include <string>
#include <time.h>
using namespace std;
using namespace dolphindb;
using namespace std::chrono;

DBConnection conn;
string hostName = "115.239.209.123";
int port = 8870;

int main(int argc, char *argv[]){
    bool ret = conn.connect(hostName, port);
    if(!ret){
        cout<<"Failed to connect to the server"<<endl;
        return 0;
    }
    cout<<"Successed to connect to the server"<<endl;

    DictionarySP t1schema = conn.run("schema(snapshotStream)");
    DictionarySP t2schema = conn.run("schema(tradeStream)");
    DictionarySP t3schema = conn.run("schema(entrustStream)");

    unordered_map<string, DictionarySP> sym2schema;
    sym2schema["snapshotStream"] = t1schema;
    sym2schema["tradeStream"] = t2schema;
    sym2schema["entrustStream"] = t3schema;
    StreamDeserializerSP sdsp = new StreamDeserializer(sym2schema);
    auto onehandler = [&](Message msg) {
            const string &symbol = msg.getSymbol();
            cout << symbol << ":";
            size_t len = msg->size();
            for (int i = 0; i < len; i++) {
                    cout << msg->get(i)->getString() << ",";
            }
            cout << endl;
    };
    int listenport = 26123;
    cout << "listenport: " << listenport << endl;
    ThreadedClient threadedClient(listenport);
    auto thread = threadedClient.subscribe(hostName, port, onehandler, "replayStreamTB", "printStreamTB", -1, true, nullptr, false, false, sdsp);

    return 0;
}
请先 登录 后评论

2 个回答

星辰大海

我想问一下,为什么我翻遍 release130 和 release200的源码,都没有找到 StreamDeserializer 相关定义呢?

请先 登录 后评论
Yating Xie

主线程需要阻塞一下,您的情况是线程创建后又立刻结束、线程崩溃了。


可以加thread->join()让程序一直运行不要退出 

    ThreadedClient threadedClient(listenport);
    auto thread = threadedClient.subscribe(hostName, port, onehandler, "replayStreamTB", "printStreamTB", -1, true, nullptr, false, false, sdsp);   
    cout<<"Successed to connect to subscribe replayStreamTB"<<endl;
    thread->join(); 



请先 登录 后评论