在一个线程上启动thrift服务器然后连接另一个线程在linux上失败

时间:2015-12-31 18:41:45

标签: c++ thrift

我正在使用Apache Thrift的C ++ api。我们按顺序调用两个函数:createServer(),然后是connectToServer()。

createServer执行此操作:

   auto serverProcessor = boost::shared_ptr<apache::thrift::TProcessor>(new ctsPluginServerProcessor(to_boost_ptr(_clientWrapper)));
   auto serverTransport = boost::shared_ptr<apache::thrift::transport::TServerTransport>(new apache::thrift::transport::TServerSocket(_serverPort));
   auto serverTransportFactory = boost::shared_ptr<apache::thrift::transport::TTransportFactory>(new apache::thrift::transport::TBufferedTransportFactory());
   auto serverProtocolFactory = boost::shared_ptr<apache::thrift::protocol::TProtocolFactory>(new apache::thrift::protocol::TBinaryProtocolFactory());
   _server = boost::shared_ptr<apache::thrift::server::TThreadedServer>(new apache::thrift::server::TThreadedServer(serverProcessor, serverTransport, serverTransportFactory, serverProtocolFactory));

   // create a new thread
   auto serverThreadFactory = unique_ptr<apache::thrift::concurrency::PlatformThreadFactory>(new apache::thrift::concurrency::PlatformThreadFactory());
   serverThreadFactory->setDetached(false);
   auto serverThreadRunner = boost::shared_ptr<apache::thrift::concurrency::Runnable>(_server);
   _serverThread = serverThreadFactory->newThread(serverThreadRunner);

   _serverThread->start();

connectToServer()执行此操作:

  _clientTransport = boost::shared_ptr<apache::thrift::transport::TSocket>(new apache::thrift::transport::TSocket("localhost", 9091));
  auto clientProtocol = boost::shared_ptr<apache::thrift::protocol::TBinaryProtocol>(new apache::thrift::protocol::TBinaryProtocol(_clientTransport));

  NILVCTM_THROW_CHECK(clientProtocol, kInvalidConnectionToCTS);
  _client = make_shared<ctsPluginServerClient>(clientProtocol);
  NILVCTM_THROW_CHECK(_clientTransport, kInvalidConnectionToCTS);
  // _clientTransport->open() fails unless I sleep here
  _clientTransport->open();
  NILVCTM_THROW_CHECK(_client, kInvalidConnectionToCTS);
  _client->connectToCTS();

此代码适用于Windows而无需睡眠 - 在Linux上,只有在注释指示的睡眠时才能使用。如何在Linux上睡不着觉?

0 个答案:

没有答案