BitMagic-C++
Public Types | Public Member Functions | Protected Member Functions
bm::thread_pool< QValue, Lock > Class Template Reference

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_typequeue_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_typeget_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...
 

Detailed Description

template<typename QValue, typename Lock>
class bm::thread_pool< QValue, Lock >

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.

Member Enumeration Documentation

◆ stop_mode

template<typename QValue , typename Lock >
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.

Member Function Documentation

◆ get_job_queue()

template<typename QValue , typename Lock >
queue_type& bm::thread_pool< QValue, Lock >::get_job_queue ( )
inlinenoexcept

Get access to the job submission queue.

Definition at line 295 of file bmthreadpool.h.

◆ is_stopped()

template<typename QValue , typename Lock >
int bm::thread_pool< QValue, Lock >::is_stopped ( ) const
inlinenoexcept

Return if thread pool is stopped by a request.

Definition at line 298 of file bmthreadpool.h.

◆ join()

template<typename QValue , typename Lock >
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().

◆ set_stop_mode()

template<typename QValue , typename Lock >
void bm::thread_pool< QValue, Lock >::set_stop_mode ( stop_mode  sm)
noexcept

Setup the criteria for threads shutdown Also notifies all threads on a new directive.

Parameters
sm- stop mode

Definition at line 378 of file bmthreadpool.h.

Referenced by bm::thread_pool< QValue, Lock >::stop().

◆ start()

template<typename QValue , typename Lock >
void bm::thread_pool< QValue, Lock >::start ( unsigned  tcount)

Start thread pool worker threads.

Parameters
tcount- number of threads to start

Definition at line 387 of file bmthreadpool.h.

References bm::thread_pool< QValue, Lock >::worker_func().

◆ stop()

template<typename QValue , typename Lock >
void bm::thread_pool< QValue, Lock >::stop ( )
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.

◆ wait_empty_queue()

template<typename QValue , typename Lock >
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.

◆ worker_func()

template<typename QValue , typename Lock >
void bm::thread_pool< QValue, Lock >::worker_func
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().


The documentation for this class was generated from the following file: