worker.qbk 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. [/
  2. Copyright Oliver Kowalke 2017.
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt
  6. ]
  7. [#worker]
  8. [section:worker Running with worker threads]
  9. [heading Keep workers running]
  10. If a worker thread is used but no fiber is created or parts of the framework
  11. (like __yield__) are touched, then no fiber scheduler is instantiated.
  12. auto worker = std::thread(
  13. []{
  14. // fiber scheduler not instantiated
  15. });
  16. worker.join();
  17. If ['use_scheduling_algorithm<>()] is invoked, the fiber scheduler is created.
  18. If the worker thread simply returns, destroys the scheduler and terminates.
  19. auto worker = std::thread(
  20. []{
  21. // fiber scheduler created
  22. boost::fibers::use_scheduling_algorithm<my_fiber_scheduler>();
  23. });
  24. worker.join();
  25. In order to keep the worker thread running, the fiber associated with the thread
  26. stack (which is called ["main] fiber) is blocked. For instance the ["main] fiber
  27. might wait on a __condition__. For a gracefully shutdown __condition__ is
  28. signalled and the ["main] fiber returns. The scheduler gets destructed if all
  29. fibers of the worker thread have been terminated.
  30. boost::fibers::mutex mtx;
  31. boost::fibers::condition_variable_any cv;
  32. auto worker = std::thread(
  33. [&mtx,&cv]{
  34. mtx.lock();
  35. // suspend till signalled
  36. cv.wait(mtx);
  37. mtx.unlock();
  38. });
  39. // signal termination
  40. cv.notify_all();
  41. worker.join();
  42. [heading Processing tasks]
  43. Tasks can be transferred via channels. The worker thread runs a pool of fibers
  44. that dequeue and executed tasks from the channel. The termination is signalled via
  45. closing the channel.
  46. using task = std::function<void()>;
  47. boost::fibers::buffered_channel<task> ch{1024};
  48. auto worker = std::thread(
  49. [&ch]{
  50. // create pool of fibers
  51. for (int i=0; i<10; ++i) {
  52. boost::fibers::fiber{
  53. [&ch]{
  54. task tsk;
  55. // dequeue and process tasks
  56. while (boost::fibers::channel_op_status::closed!=ch.pop(tsk)){
  57. tsk();
  58. }
  59. }}.detach();
  60. }
  61. task tsk;
  62. // dequeue and process tasks
  63. while (boost::fibers::channel_op_status::closed!=ch.pop(tsk)){
  64. tsk();
  65. }
  66. });
  67. // feed channel with tasks
  68. ch.push([]{ ... });
  69. ...
  70. // signal termination
  71. ch.close();
  72. worker.join();
  73. An alternative is to use a work-stealing scheduler. This kind of scheduling
  74. algorithm a worker thread steals fibers from the ready-queue of other worker
  75. threads if its own ready-queue is empty.
  76. [note Wait till all worker threads have registered the work-stealing scheduling
  77. algorithm.]
  78. boost::fibers::mutex mtx;
  79. boost::fibers::condition_variable_any cv;
  80. // start wotrker-thread first
  81. auto worker = std::thread(
  82. [&mtx,&cv]{
  83. boost::fibers::use_scheduling_algorithm<boost::fibers::algo::work_stealing>(2);
  84. mtx.lock();
  85. // suspend main-fiber from the worker thread
  86. cv.wait(mtx);
  87. mtx.unlock();
  88. });
  89. boost::fibers::use_scheduling_algorithm<boost::fibers::algo::work_stealing>(2);
  90. // create fibers with tasks
  91. boost::fibers::fiber f{[]{ ... }};
  92. ...
  93. // signal termination
  94. cv.notify_all();
  95. worker.join();
  96. [important Because the TIB (thread information block on Windows) is not fully
  97. described in the MSDN, it might be possible that not all required TIB-parts are
  98. swapped. Using WinFiber implementation might be an alternative (see documentation
  99. about [@http://www.boost.org/doc/libs/1_65_1/libs/context/doc/html/context/cc/implementations__fcontext_t__ucontext_t_and_winfiber.html
  100. ['implementations fcontext_t, ucontext_t and WinFiber of boost.context]]).]
  101. [endsect]