fork(1) download
  1. #include <iostream>
  2. #include <thread>
  3.  
  4. #include <boost/asio.hpp>
  5.  
  6. using boost::asio::io_service;
  7.  
  8. class foo
  9. {
  10. public:
  11. foo(io_service& service)
  12. : service_(service) {}
  13.  
  14. void bar()
  15. {
  16. service_.post(
  17. [this]
  18. {
  19. std::cout << __PRETTY_FUNCTION__ << std::endl;
  20. sleep(1);
  21. });
  22. }
  23. private:
  24. io_service& service_;
  25. };
  26.  
  27. int main()
  28. {
  29. io_service service;
  30. io_service::work* work = new io_service::work(service);
  31. std::thread t([&service] { service.run(); });
  32.  
  33. foo f(service);
  34. for (size_t i = 0; i < 10; ++i)
  35. f.bar();
  36. sleep(5);
  37. std::cout << "Closing" << std::endl;
  38.  
  39. //delete work;
  40. //work = nullptr;
  41.  
  42. service.stop();
  43. t.join();
  44. return 0;
  45. }
  46.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty