fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <thread>
  4. #include <string>
  5. #include <chrono>
  6. #include <random>
  7. #include <condition_variable>
  8. #include <boost/asio.hpp>
  9.  
  10.  
  11. struct worker
  12. {
  13. worker(int index)
  14. : _index(index)
  15. {}
  16.  
  17. void operator()() const
  18. {
  19. printf( "I am worker: %d\n", _index);
  20. }
  21.  
  22. int _index;
  23. };
  24.  
  25. struct worker_pool
  26. {
  27. boost::asio::io_service _io_service;
  28. boost::asio::io_service::work _work { _io_service };
  29.  
  30. std::vector<std::thread> _threads;
  31.  
  32. std::condition_variable _cv;
  33. std::mutex _cvm;
  34. size_t _tasks = 0;
  35.  
  36.  
  37. void start()
  38. {
  39. for (int i = 0 ; i < 8 ; ++i) {
  40. _threads.emplace_back(std::bind(&worker_pool::thread_proc, this));
  41. }
  42. }
  43.  
  44. void wait()
  45. {
  46. std::unique_lock<std::mutex> lock(_cvm);
  47. _cv.wait(lock, [this] { return _tasks == 0; });
  48. }
  49.  
  50. void stop()
  51. {
  52. wait();
  53. _io_service.stop();
  54. for (auto& t : _threads) {
  55. if (t.joinable())
  56. t.join();
  57. }
  58. _threads.clear();
  59.  
  60. }
  61.  
  62. void thread_proc()
  63. {
  64. while (!_io_service.stopped())
  65. {
  66. _io_service.run();
  67. }
  68. }
  69.  
  70. void reduce() {
  71.  
  72. std::unique_lock<std::mutex> lock(_cvm);
  73. if (--_tasks == 0) {
  74. lock.unlock();
  75. _cv.notify_all();
  76. }
  77. }
  78.  
  79. void submit( worker & f)
  80. {
  81. std::unique_lock<std::mutex> lock(_cvm);
  82. ++ _tasks;
  83. lock.unlock();
  84. _io_service.post([this, &f]
  85. {
  86. f();
  87. reduce();
  88. });
  89. }
  90. };
  91.  
  92. auto main() -> int
  93. {
  94. worker_pool pool;
  95. pool.start();
  96.  
  97. for (int i = 0 ; i < 100 ; ++i)
  98. {
  99. worker job(i);
  100. pool.submit(job );
  101. }
  102.  
  103. pool.wait();
  104. pool.stop();
  105.  
  106. return 0;
  107. }
  108.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/home/y5pjfk/cc3S34lw.o: In function `boost::asio::detail::task_io_service::shutdown_service()':
prog.cpp:(.text._ZN5boost4asio6detail15task_io_service16shutdown_serviceEv[_ZN5boost4asio6detail15task_io_service16shutdown_serviceEv]+0x6c): undefined reference to `boost::system::system_category()'
/home/y5pjfk/cc3S34lw.o: In function `boost::asio::detail::task_io_service::~task_io_service()':
prog.cpp:(.text._ZN5boost4asio6detail15task_io_serviceD2Ev[_ZN5boost4asio6detail15task_io_serviceD5Ev]+0x32): undefined reference to `boost::system::system_category()'
/home/y5pjfk/cc3S34lw.o: In function `boost::asio::detail::task_io_service::~task_io_service()':
prog.cpp:(.text._ZN5boost4asio6detail15task_io_serviceD0Ev[_ZN5boost4asio6detail15task_io_serviceD5Ev]+0x32): undefined reference to `boost::system::system_category()'
/home/y5pjfk/cc3S34lw.o: In function `boost::asio::detail::task_io_service_thread_info::~task_io_service_thread_info()':
prog.cpp:(.text._ZN5boost4asio6detail27task_io_service_thread_infoD2Ev[_ZN5boost4asio6detail27task_io_service_thread_infoD5Ev]+0x32): undefined reference to `boost::system::system_category()'
/home/y5pjfk/cc3S34lw.o: In function `worker_pool::thread_proc()':
prog.cpp:(.text._ZN11worker_pool11thread_procEv[_ZN11worker_pool11thread_procEv]+0x45): undefined reference to `boost::system::system_category()'
/home/y5pjfk/cc3S34lw.o:prog.cpp:(.text._ZN11worker_pool11thread_procEv[_ZN11worker_pool11thread_procEv]+0x56): more undefined references to `boost::system::system_category()' follow
/home/y5pjfk/cc3S34lw.o: In function `_GLOBAL__sub_I_main':
prog.cpp:(.text.startup+0x685): undefined reference to `boost::system::generic_category()'
prog.cpp:(.text.startup+0x68a): undefined reference to `boost::system::generic_category()'
prog.cpp:(.text.startup+0x68f): undefined reference to `boost::system::system_category()'
prog.cpp:(.text.startup+0x694): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty