36 Worker(std::mutex& queue_mutex, std::deque<ThreadPool::Task>& queue,
37 std::atomic<bool>& run_flag, std::atomic<bool>& sleeping_flag,
38 std::atomic<bool>& done_flag,
unsigned int empty_queue_wait_time,
39 std::exception_ptr& exception_ptr)
49 std::unique_ptr<ThreadPool::Task> task_ptr =
nullptr;
52 task_ptr = make_unique<ThreadPool::Task>(
m_queue.get().front());
78 std::reference_wrapper<std::deque<ThreadPool::Task>>
m_queue;
90 : m_worker_run_flags(thread_count), m_worker_sleeping_flags(thread_count),
92 for (
unsigned int i = 0; i < thread_count; ++i) {
103 void waitWorkers(std::vector<std::atomic<bool>>& worker_flags,
unsigned int wait_time) {
105 for (
auto& flag : worker_flags) {
107 std::this_thread::sleep_for(std::chrono::milliseconds(wait_time));
127 bool queue_is_empty =
false;
130 queue_is_empty =
m_queue.empty();
132 if (!queue_is_empty) {
155 m_queue.emplace_back(std::move(task));
std::deque< Task > m_queue
void submit(Task task)
Submit a task to be executed.
std::vector< std::atomic< bool > > m_worker_sleeping_flags
std::vector< std::atomic< bool > > m_worker_run_flags
ThreadPool(unsigned int thread_count=std::thread::hardware_concurrency(), unsigned int empty_queue_wait_time=50)
Constructs a new ThreadPool.
bool checkForException(bool rethrow=false)
Checks if any task has thrown an exception and optionally rethrows it.
std::reference_wrapper< std::exception_ptr > m_exception_ptr
std::reference_wrapper< std::mutex > m_queue_mutex
std::vector< std::atomic< bool > > m_worker_done_flags
unsigned int m_empty_queue_wait_time
unsigned int m_empty_queue_wait_time
std::reference_wrapper< std::atomic< bool > > m_done_flag
std::exception_ptr m_exception_ptr
std::reference_wrapper< std::atomic< bool > > m_run_flag
std::reference_wrapper< std::atomic< bool > > m_sleeping_flag
std::reference_wrapper< std::deque< ThreadPool::Task > > m_queue
std::function< void(void)> Task
The type of tasks the pool can execute.