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_pool
  12. {
  13. boost::asio::io_service _io_service;
  14. boost::asio::io_service::work _work { _io_service };
  15.  
  16. std::vector<std::thread> _threads;
  17.  
  18. std::condition_variable _cv;
  19. std::mutex _cvm;
  20. size_t _tasks = 0;
  21.  
  22.  
  23. void start()
  24. {
  25. for (int i = 0 ; i < 8 ; ++i) {
  26. _threads.emplace_back(std::bind(&worker_pool::thread_proc, this));
  27. }
  28. }
  29.  
  30. void wait()
  31. {
  32. std::unique_lock<std::mutex> lock(_cvm);
  33. _cv.wait(lock, [this] { return _tasks == 0; });
  34. }
  35.  
  36. void stop()
  37. {
  38. wait();
  39. _io_service.stop();
  40. for (auto& t : _threads) {
  41. if (t.joinable())
  42. t.join();
  43. }
  44. _threads.clear();
  45.  
  46. }
  47.  
  48. void thread_proc()
  49. {
  50. while (!_io_service.stopped())
  51. {
  52. _io_service.run();
  53. }
  54. }
  55.  
  56. void reduce() {
  57.  
  58. std::unique_lock<std::mutex> lock(_cvm);
  59. if (--_tasks == 0) {
  60. lock.unlock();
  61. _cv.notify_all();
  62. }
  63. }
  64.  
  65. template<class F>
  66. void submit(F&& f)
  67. {
  68. std::unique_lock<std::mutex> lock(_cvm);
  69. ++ _tasks;
  70. lock.unlock();
  71. _io_service.post([this, f = std::forward<F>(f)]
  72. {
  73. f();
  74. reduce();
  75. });
  76.  
  77.  
  78. }
  79. };
  80.  
  81. struct some_other_work
  82. {
  83. some_other_work(int index, std::chrono::milliseconds delay)
  84. : _index(index), _delay(delay)
  85. {}
  86.  
  87. void operator()() const {
  88. std::this_thread::sleep_for(_delay);
  89. printf( "I am worker: %d\n", _index);
  90. }
  91.  
  92. int _index;
  93. std::chrono::milliseconds _delay;
  94. };
  95.  
  96. auto main() -> int
  97. {
  98. worker_pool pool;
  99. pool.start();
  100.  
  101. for (int i = 0 ; i < 100 ; ++i)
  102. {
  103. std::chrono::milliseconds delay(500);
  104. pool.submit(some_other_work(i, delay));
  105. }
  106.  
  107. pool.wait();
  108. pool.stop();
  109.  
  110. return 0;
  111. }
  112.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/home/7Jhm7a/ccGu6O2h.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/7Jhm7a/ccGu6O2h.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/7Jhm7a/ccGu6O2h.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/7Jhm7a/ccGu6O2h.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/7Jhm7a/ccGu6O2h.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/7Jhm7a/ccGu6O2h.o:prog.cpp:(.text._ZN11worker_pool11thread_procEv[_ZN11worker_pool11thread_procEv]+0x56): more undefined references to `boost::system::system_category()' follow
/home/7Jhm7a/ccGu6O2h.o: In function `_GLOBAL__sub_I_main':
prog.cpp:(.text.startup+0x695): undefined reference to `boost::system::generic_category()'
prog.cpp:(.text.startup+0x69a): undefined reference to `boost::system::generic_category()'
prog.cpp:(.text.startup+0x69f): undefined reference to `boost::system::system_category()'
prog.cpp:(.text.startup+0x6a4): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty