Dispatch a function, executing it immediately if the queue is already loaded. Otherwise adds the function to the queue and wakes a thread.
| 119 | // Dispatch a function, executing it immediately if the queue is already |
| 120 | // loaded. Otherwise adds the function to the queue and wakes a thread. |
| 121 | void do_execute(std::shared_ptr<function_base> p, |
| 122 | const std::shared_ptr<std::size_t>& work_count) |
| 123 | { |
| 124 | std::unique_lock<std::mutex> lock(mutex_); |
| 125 | if (queue_.size() > thread_count_ * 16) |
| 126 | { |
| 127 | do_work_started(work_count); |
| 128 | lock.unlock(); |
| 129 | execute(lock, p); |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | queue_.push(p); |
| 134 | do_work_started(work_count); |
| 135 | condition_.notify_one(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Ask all threads to shut down. |
| 140 | void stop_threads() |