BitMagic-C++
|
Thread pool with custom (thread safe) queue. More...
#include <bmthreadpool.h>
Public Types | |
enum | stop_mode { no_stop = 0 , stop_when_done = 1 , stop_now = 2 } |
Stop modes for threads: 0 - keep running/waiting for jobs 1 - wait for empty task queue then stop threads 2 - stop threads now even if there are pending tasks. More... | |
typedef QValue | value_type |
typedef Lock | lock_type |
typedef bm::queue_sync< QValue, lock_type > | queue_type |
Public Member Functions | |
thread_pool (stop_mode sm=no_stop) noexcept | |
~thread_pool () | |
void | set_stop_mode (stop_mode sm) noexcept |
Setup the criteria for threads shutdown Also notifies all threads on a new directive. More... | |
void | stop () noexcept |
Request an immediate stop of all threads in the pool. More... | |
void | start (unsigned tcount) |
Start thread pool worker threads. More... | |
void | join () |
Wait for threads to finish (or stop if stop was requested) More... | |
void | wait_empty_queue () |
Conditional spin-wait for the queue to empty (Important note: tasks may still be running, but the queue is empty) More... | |
queue_type & | get_job_queue () noexcept |
Get access to the job submission queue. More... | |
int | is_stopped () const noexcept |
Return if thread pool is stopped by a request. More... | |
Protected Member Functions | |
void | worker_func () |
Internal worker wrapper with busy-wait spin loop making pthread-like call for tasks. More... | |
Thread pool with custom (thread safe) queue.
Thread pool implements a busy-wait task stealing design pattern
QValue - task queue value parameter Lock - locking protection type (like std::mutex or spinlock)
Definition at line 238 of file bmthreadpool.h.
enum bm::thread_pool::stop_mode |
Stop modes for threads: 0 - keep running/waiting for jobs 1 - wait for empty task queue then stop threads 2 - stop threads now even if there are pending tasks.
Enumerator | |
---|---|
no_stop | keep spinning on busy-wait |
stop_when_done | stop if tsak queue is empty |
stop_now | stop right now |
Definition at line 251 of file bmthreadpool.h.
|
inlinenoexcept |
Get access to the job submission queue.
Definition at line 295 of file bmthreadpool.h.
|
inlinenoexcept |
Return if thread pool is stopped by a request.
Definition at line 298 of file bmthreadpool.h.
void bm::thread_pool< QValue, Lock >::join |
Wait for threads to finish (or stop if stop was requested)
Definition at line 403 of file bmthreadpool.h.
References bm::join_multiple_threads().
|
noexcept |
Setup the criteria for threads shutdown Also notifies all threads on a new directive.
sm | - stop mode |
Definition at line 378 of file bmthreadpool.h.
Referenced by bm::thread_pool< QValue, Lock >::stop().
void bm::thread_pool< QValue, Lock >::start | ( | unsigned | tcount | ) |
Start thread pool worker threads.
tcount | - number of threads to start |
Definition at line 387 of file bmthreadpool.h.
References bm::thread_pool< QValue, Lock >::worker_func().
|
inlinenoexcept |
Request an immediate stop of all threads in the pool.
Definition at line 274 of file bmthreadpool.h.
References bm::thread_pool< QValue, Lock >::set_stop_mode(), and bm::thread_pool< QValue, Lock >::stop_now.
void bm::thread_pool< QValue, Lock >::wait_empty_queue |
Conditional spin-wait for the queue to empty (Important note: tasks may still be running, but the queue is empty)
Definition at line 412 of file bmthreadpool.h.
|
protected |
Internal worker wrapper with busy-wait spin loop making pthread-like call for tasks.
Definition at line 437 of file bmthreadpool.h.
References bm::task_descr::argp, BM_ASSERT, bm::task_descr::done, bm::task_descr::err_code, and bm::task_descr::func.
Referenced by bm::thread_pool< QValue, Lock >::start().