|
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. | |
| void | stop () noexcept |
| Request an immediate stop of all threads in the pool. | |
| void | start (unsigned tcount) |
| Start thread pool worker threads. | |
| void | join () |
| Wait for threads to finish (or stop if stop was requested). | |
| void | wait_empty_queue () |
| Conditional spin-wait for the queue to empty (Important note: tasks may still be running, but the queue is empty). | |
| queue_type & | get_job_queue () noexcept |
| Get access to the job submission queue. | |
| int | is_stopped () const noexcept |
| Return if thread pool is stopped by a request. | |
Protected Member Functions | |
| void | worker_func () |
| Internal worker wrapper with busy-wait spin loop making pthread-like call for tasks. | |
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.
| typedef Lock bm::thread_pool< QValue, Lock >::lock_type |
Definition at line 242 of file bmthreadpool.h.
| typedef bm::queue_sync<QValue, lock_type> bm::thread_pool< QValue, Lock >::queue_type |
Definition at line 243 of file bmthreadpool.h.
| typedef QValue bm::thread_pool< QValue, Lock >::value_type |
Definition at line 241 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 |
Definition at line 259 of file bmthreadpool.h.
References no_stop.
| bm::thread_pool< QValue, Lock >::~thread_pool | ( | ) |
Definition at line 367 of file bmthreadpool.h.
References join(), set_stop_mode(), and stop_when_done.
|
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.
Referenced by wait_empty_queue(), and worker_func().
| 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().
Referenced by ~thread_pool().
|
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 stop(), and ~thread_pool().
| 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 stop_now, and worker_func().
|
inlinenoexcept |
Request an immediate stop of all threads in the pool.
Definition at line 274 of file bmthreadpool.h.
References set_stop_mode(), and 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.
References is_stopped(), and stop_now.
|
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, bm::task_descr::func, is_stopped(), and stop_now.
Referenced by start().