Constructor launches the requested number of workers.
| 44 | public: |
| 45 | /// Constructor launches the requested number of workers. |
| 46 | explicit ThreadPool( |
| 47 | size_t threads_number = std::thread::hardware_concurrency()) |
| 48 | : stop_(false) { |
| 49 | assert(threads_number && "There must be 1 or more threads"); |
| 50 | workers_.reserve(threads_number); |
| 51 | for(; threads_number; --threads_number) { |
| 52 | workers_.emplace_back([this] { |
| 53 | while (true) { |
| 54 | std::function<void()> task; |
| 55 | { |
| 56 | std::unique_lock<std::mutex> lock(mutex_); |
| 57 | condition_.wait(lock, [this]{ return stop_ || !tasks_.empty(); }); |
| 58 | if (stop_ && tasks_.empty()) { |
| 59 | return; |
| 60 | } |
| 61 | task = std::move(tasks_.front()); |
| 62 | tasks_.pop(); |
| 63 | } |
| 64 | task(); |
| 65 | } |
| 66 | }); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | ThreadPool(const ThreadPool&) = delete; |
| 71 | ThreadPool& operator=(const ThreadPool&) = delete; |
no outgoing calls