The constructor starts a thread pool with the specified number of threads. Note that the thread_count is not a fixed limit on the pool's concurrency. Additional threads may temporarily be added to the pool if they join a fork_executor.
| 22 | // Additional threads may temporarily be added to the pool if they join a |
| 23 | // fork_executor. |
| 24 | explicit fork_join_pool( |
| 25 | std::size_t thread_count = std::max(std::thread::hardware_concurrency(), 1u) * 2) |
| 26 | : use_count_(1), |
| 27 | threads_(thread_count) |
| 28 | { |
| 29 | try |
| 30 | { |
| 31 | // Ask each thread in the pool to dequeue and execute functions until |
| 32 | // it is time to shut down, i.e. the use count is zero. |
| 33 | for (thread_count_ = 0; thread_count_ < thread_count; ++thread_count_) |
| 34 | { |
| 35 | threads_.executor().execute( |
| 36 | [this] |
| 37 | { |
| 38 | std::unique_lock<std::mutex> lock(mutex_); |
| 39 | while (use_count_ > 0) |
| 40 | if (!execute_next(lock)) |
| 41 | condition_.wait(lock); |
| 42 | }); |
| 43 | } |
| 44 | } |
| 45 | catch (...) |
| 46 | { |
| 47 | stop_threads(); |
| 48 | threads_.wait(); |
| 49 | throw; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // The destructor waits for the pool to finish executing functions. |
| 54 | ~fork_join_pool() |