decimusphostle

Thrift Server Issue - Main Thread

Nov 21st, 2011
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. //Pseudo-code
  2.  
  3. //Main thread:
  4. class daemon //Meant to be a singleton - mostly for signal-handling reasons
  5. {
  6. public:
  7. daemon()
  8. : _signal_received(false),
  9. _thrift_server_thread(_shared_state),
  10. _background_thread(_shared_state) {}
  11.  
  12. void main_loop()
  13. {
  14. while(!_signal_received)
  15. {
  16. //Any suggestions on how to address signal-handling
  17. //without having a spinning thread?
  18. }
  19.  
  20. //Stop server(thread)
  21.  
  22. //Previously I was directly invoking _server->stop() here
  23. //on a TSimpleServer from the main thread. This was causing the
  24. //error as noted in the SO question(http://goo.gl/3yBhV).
  25. //I resolved this by adding an endpoint to the Thrift service
  26. //primarily to stop the server in response to an invocation.
  27. //This seems far from ideal, but I couldn't find a better solution.
  28. //Any suggestions?
  29. stop_server_thread();
  30.  
  31. //Stop background thread
  32. stop_background_thread();
  33. }
  34.  
  35. static void signal_handler(int signal)
  36. {
  37. _signal_received = true;
  38. }
  39.  
  40. private:
  41. static bool _signal_received;
  42.  
  43. void stop_server_thread()
  44. {
  45. thrift_client_request_to_stop_server()
  46.  
  47. _server_thread.join();
  48. }
  49.  
  50. void stop_background_thread()
  51. {
  52. _background_thread.interrupt();
  53.  
  54. _background_thread.join();
  55. }
  56.  
  57. void thrift_client_request_to_stop_server()
  58. {
  59. boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
  60. boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
  61. boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
  62.  
  63. thrift_client client(protocol);
  64. transport->open();
  65. client.stop_server();
  66. transport->close();
  67. }
  68. };
Advertisement
Add Comment
Please, Sign In to add comment