把Proactor与Reactor事件集成的演示代码

前端之家收集整理的这篇文章主要介绍了把Proactor与Reactor事件集成的演示代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://www.jb51.cc/article/p-oiyvzxyj-op.html

  1. //file: main.cpp
  2. //author: StoneJiang http://www.tao-studio.net
  3. //date: 2008-12-14
  4. //desc: Integrating Proactor and Reactor Events on Windows
  5.  
  6.  
  7. #include "ace/Proactor.h"
  8. #include "ace/WIN32_Proactor.h"
  9. #include "ace/Atomic_Op.h"
  10. #include "ace/OS_NS_unistd.h"
  11. #include "ace/Reactor.h"
  12. #include "ace/Time_Value.h"
  13.  
  14. class Timeout_Handler : public ACE_Handler,public ACE_Event_Handler
  15. {
  16. // = TITLE
  17. // Generic timeout handler.
  18.  
  19. public:
  20. Timeout_Handler (void)
  21. {
  22. }
  23.  
  24. // This is called by the Proactor. This is declared in ACE_Handler.
  25. virtual void handle_time_out (const ACE_Time_Value &tv,const void *arg)
  26. {
  27. // Print out when timeouts occur.
  28. ACE_DEBUG ((LM_DEBUG,"(%t|%P) %d timeout occurred for %s @ %d./n",++count_,(char *) arg,tv.sec ()));
  29.  
  30. // Since there is only one thread that can do the timeouts in
  31. // Reactor,lets keep the handle_timeout short for that
  32. // thread.
  33. if (ACE_OS::strcmp ((char *) arg,"Proactor") == 0)
  34. // Sleep for a while
  35. ACE_OS::sleep (1);
  36. }
  37.  
  38. // This method is declared in ACE_Event_Handler.
  39. virtual int handle_timeout (const ACE_Time_Value &tv,const void *arg)
  40. {
  41. this->handle_time_out (tv,arg);
  42. return 0;
  43. }
  44.  
  45. private:
  46. ACE_Atomic_Op int> count_;
  47. };
  48.  
  49.  
  50. int
  51. ACE_TMAIN (int,ACE_TCHAR *[])
  52. {
  53. ACE_DEBUG ((LM_DEBUG,"(%t|%P) work starup/n"));
  54. ACE_Proactor::close_singleton ();
  55.  
  56. ACE_WIN32_Proactor *impl = new ACE_WIN32_Proactor (0,1);
  57. ACE_Proactor::instance (new ACE_Proactor (impl,1),1);
  58.  
  59. ACE_Reactor::instance ()->register_handler(impl,impl->get_handle ());
  60.  
  61. Timeout_Handler handler;
  62. // Register a 2 second timer.
  63. ACE_Time_Value foo_tv (2);
  64. if (ACE_Proactor::instance()->schedule_timer (handler,(void *) "Proactor",ACE_Time_Value::zero,foo_tv) == -1)
  65. ACE_ERROR_RETURN ((LM_ERROR,"%p/n","schedule_timer"),-1);
  66.  
  67. // Register a 3 second timer.
  68. ACE_Time_Value bar_tv (3);
  69. if (ACE_Reactor::instance ()->schedule_timer (&handler,(void *) "Reactor",bar_tv) == -1)
  70. ACE_ERROR_RETURN ((LM_ERROR,-1);
  71.  
  72.  
  73. ACE_Reactor::instance()->run_event_loop();
  74. ACE_Reactor::instance ()->remove_handler (impl,ACE_Event_Handler::DONT_CALL);
  75. ACE_DEBUG ((LM_DEBUG,"(%t|%P) work complete/n"));
  76. return 0;
  77. }

猜你在找的React相关文章