40 : m_queue_mutex(queue_mutex)
42 , m_run_flag(run_flag)
43 , m_sleeping_flag(sleeping_flag)
44 , m_done_flag(done_flag)
45 , m_empty_queue_wait_time(empty_queue_wait_time)
46 , m_exception_ptr(exception_ptr) {}
49 while (m_run_flag.get()) {
55 if (m_exception_ptr !=
nullptr) {
59 if (!m_queue.get().empty()) {
61 m_queue.get().pop_front();
74 m_sleeping_flag.get() =
true;
76 m_sleeping_flag.get() =
false;
80 m_sleeping_flag.get() =
true;
81 m_done_flag.get() =
true;
82 m_run_flag.get() =
false;
91 unsigned int m_empty_queue_wait_time;
98 : m_worker_run_flags(thread_count)
99 , m_worker_sleeping_flags(thread_count)
100 , m_worker_done_flags(thread_count)
101 , m_empty_queue_wait_time(empty_queue_wait_time) {
102 for (
unsigned int i = 0; i < thread_count; ++i) {
115 for (
auto& flag : worker_flags) {
154 bool queue_is_empty =
false;
155 while (!queue_is_empty) {
161 queue_is_empty =
m_queue.empty();
163 if (!queue_is_empty) {
void submit(Task task)
Submit a task to be executed.
std::deque< Task > m_queue
size_t running() const
Return the number of running tasks.
void block(bool throw_on_exception=true)
std::vector< std::atomic< bool > > m_worker_sleeping_flags
size_t queued() const
Return the number of queued tasks.
unsigned int m_empty_queue_wait_time
std::vector< std::thread > m_workers
std::vector< std::atomic< bool > > m_worker_run_flags
std::vector< std::atomic< bool > > m_worker_done_flags
bool checkForException(bool rethrow=false)
Checks if any task has thrown an exception and optionally rethrows it.
std::exception_ptr m_exception_ptr
size_t activeThreads() const
Return the number of active workers (either running or sleeping)
ThreadPool(unsigned int thread_count=std::thread::hardware_concurrency(), unsigned int empty_queue_wait_time=50)
Constructs a new ThreadPool.
T current_exception(T... args)
T emplace_back(T... args)
std::unique_ptr< T > make_unique(Args &&... args)
Constructs an object of type T and wraps it in a std::unique_ptr using args as the parameter list for...
T rethrow_exception(T... args)