fork download
  1. //-------------------------------------------
  2. // HEADER
  3. //-------------------------------------------
  4.  
  5. #include <functional>
  6. #include <system_error>
  7. #include <thread>
  8.  
  9. #include <boost/asio.hpp>
  10.  
  11. using boost::asio::io_service;
  12.  
  13. class service
  14. {
  15. public:
  16. typedef std::function<void (const std::error_code&)> completion_handler;
  17.  
  18. service();
  19. ~service();
  20.  
  21. void start(completion_handler handle_start);
  22. void stop(completion_handler handle_stop);
  23.  
  24. template <typename HandlerFunction>
  25. void post(HandlerFunction perform_operation)
  26. {
  27. strand_.post(perform_operation);
  28. }
  29. private:
  30. typedef std::shared_ptr<io_service> service_ptr;
  31.  
  32. static void run_service(service_ptr core_service);
  33.  
  34. service_ptr core_service_;
  35. io_service::work* work_;
  36. io_service::strand strand_;
  37. std::thread runner_;
  38. };
  39.  
  40. //-------------------------------------------
  41. // TRANSLATION UNIT
  42. //-------------------------------------------
  43.  
  44. #define BITCOIN_ASSERT BOOST_ASSERT
  45.  
  46. service::service()
  47. : core_service_(std::make_shared<io_service>()),
  48. strand_(*core_service_), work_(nullptr)
  49. {
  50. }
  51. service::~service()
  52. {
  53. BITCOIN_ASSERT(work_ == nullptr);
  54. core_service_->stop();
  55. runner_.join();
  56. }
  57.  
  58. void service::run_service(service_ptr core_service)
  59. {
  60. core_service->run();
  61. std::cout << "service closed" << std::endl;
  62. }
  63.  
  64. void service::start(completion_handler handle_start)
  65. {
  66. BITCOIN_ASSERT(work_ == nullptr);
  67. work_ = new io_service::work(*core_service_);
  68. runner_ = std::thread(&service::run_service, core_service_);
  69. post(std::bind(handle_start, std::error_code()));
  70. }
  71. void service::stop(completion_handler handle_stop)
  72. {
  73. BITCOIN_ASSERT(work_ != nullptr);
  74. delete work_;
  75. work_ = nullptr;
  76. post(std::bind(handle_stop, std::error_code()));
  77. }
  78.  
  79. //-------------------------------------------
  80. // TEST FILE
  81. //-------------------------------------------
  82.  
  83. #include <iostream>
  84.  
  85. service* s = new service;
  86.  
  87. void foo()
  88. {
  89. std::cout << "foo called" << std::endl;
  90. }
  91.  
  92. void bar()
  93. {
  94. std::cout << "bar called" << std::endl;
  95. }
  96.  
  97. void handle_stop(const std::error_code& ec)
  98. {
  99. //s->post(foo);
  100. delete s;
  101. }
  102. void stop()
  103. {
  104. std::cout << "stop called" << std::endl;
  105. s->stop(handle_stop);
  106. }
  107.  
  108. void begin(const std::error_code& ec)
  109. {
  110. std::cout << "Started" << std::endl;
  111. s->post(foo);
  112. s->post(bar);
  113. s->post(stop);
  114. }
  115.  
  116. int main()
  117. {
  118. s->start(begin);
  119. std::cin.get();
  120. return 0;
  121. }
  122.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:9:26: fatal error: boost/asio.hpp: No such file or directory
compilation terminated.
stdout
Standard output is empty