Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Pseudo-code
- //Main thread:
- class daemon //Meant to be a singleton - mostly for signal-handling reasons
- {
- public:
- daemon()
- : _signal_received(false),
- _thrift_server_thread(_shared_state),
- _background_thread(_shared_state) {}
- void main_loop()
- {
- while(!_signal_received)
- {
- //Any suggestions on how to address signal-handling
- //without having a spinning thread?
- }
- //Stop server(thread)
- //Previously I was directly invoking _server->stop() here
- //on a TSimpleServer from the main thread. This was causing the
- //error as noted in the SO question(http://goo.gl/3yBhV).
- //I resolved this by adding an endpoint to the Thrift service
- //primarily to stop the server in response to an invocation.
- //This seems far from ideal, but I couldn't find a better solution.
- //Any suggestions?
- stop_server_thread();
- //Stop background thread
- stop_background_thread();
- }
- static void signal_handler(int signal)
- {
- _signal_received = true;
- }
- private:
- static bool _signal_received;
- void stop_server_thread()
- {
- thrift_client_request_to_stop_server()
- _server_thread.join();
- }
- void stop_background_thread()
- {
- _background_thread.interrupt();
- _background_thread.join();
- }
- void thrift_client_request_to_stop_server()
- {
- boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
- boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
- boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
- thrift_client client(protocol);
- transport->open();
- client.stop_server();
- transport->close();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment