【发布时间】:2019-05-23 21:04:39
【问题描述】:
我想在单独的线程中使用TTheadedServer 来控制何时停止/启动它。我的应用程序只需要 1 个控制线程和 1 个处理线程。我不希望有多个客户,因为我使用 Thrift 作为中继。 TSimpleServer 不是线程安全的,所以我放弃了这个选项。
我做了一个小例子来检查它是否是线程安全的,并使用 clang 的 thread-sanitizer 来确保它是线程安全的。这是示例
std::shared_ptr<MyHandler> handler = std::make_shared<MyHandler>();
int port = 9090;
th::stdcxx::shared_ptr<th::TProcessor> processor(new HandlerProcessor(handler));
th::stdcxx::shared_ptr<tht::TServerTransport> serverTransport(new tht::TServerSocket(port));
th::stdcxx::shared_ptr<tht::TTransportFactory> transportFactory(
new tht::TBufferedTransportFactory());
th::stdcxx::shared_ptr<thp::TProtocolFactory> protocolFactory(new thp::TBinaryProtocolFactory());
ths::TThreadedServer server(processor, serverTransport, transportFactory, protocolFactory);
// start in another thread
std::thread t(&ths::TThreadedServer::serve, &server);
t.detach();
std::this_thread::sleep_for(std::chrono::seconds(5));
// stop in this thread
server.stop();
std::this_thread::sleep_for(std::chrono::seconds(5));
所以我要做的就是在另一个线程中用serve() 启动服务器,然后等待一段时间,然后停止它。我用线程清理器运行了这个,并收到了一些线程安全警告。我在这里提到2:
第一:thrift/lib/cpp/src/thrift/transport/TServerSocket.cpp:244,在:
interruptableChildren_ = enable;
第二:thrift/lib/cpp/src/thrift/transport/TServerSocket.cpp:654,在:
if (-1 == send(notifySocket, cast_sockopt(&byte), sizeof(int8_t), 0)) {
GlobalOutput.perror("TServerSocket::notify() send() ", THRIFT_GET_SOCKET_ERROR);
}
那么我做的对吗? TThreadedServer 控制器线程安全吗? Thread-sanitizer 似乎不这么认为,尽管测试程序可以正常运行。
我正在使用 Thrift 0.12.0。
【问题讨论】:
标签: c++ multithreading c++11 rpc thrift