bm::thread_pool< QValue, Lock > Class Template Reference
BitMagic Library » Task parallel programming

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.

## 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 Typedef Documentation

## lock_type

template<typename QValue, typename Lock>

typedef Lock bm::thread_pool< QValue, Lock >::lock_type

Definition at line 242 of file bmthreadpool.h.

## queue_type

template<typename QValue, typename Lock>

typedef bm::queue_sync<QValue, lock_type> bm::thread_pool< QValue, Lock >::queue_type

Definition at line 243 of file bmthreadpool.h.

## value_type

template<typename QValue, typename Lock>

typedef QValue bm::thread_pool< QValue, Lock >::value_type

Definition at line 241 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.

## Constructor & Destructor Documentation

## thread_pool()

template<typename QValue, typename Lock>

bm::thread_pool< QValue, Lock >::thread_pool | ( | stop_mode | sm = no_stop | ) |

inline noexcept

Definition at line 259 of file bmthreadpool.h.

References no_stop.

## ~thread_pool()

template<typename QValue, typename Lock>

bm::thread_pool< QValue, Lock >::~thread_pool | ( |  | ) |

Definition at line 367 of file bmthreadpool.h.

References join(), set_stop_mode(), and stop_when_done.

## Member Function Documentation

## get_job_queue()

template<typename QValue, typename Lock>

queue_type & bm::thread_pool< QValue, Lock >::get_job_queue | ( |  | ) |

inline noexcept

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

inline noexcept

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().

## 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().

Referenced by ~thread_pool().

## 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 stop(), and ~thread_pool().

## 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 stop_now, and worker_func().

## stop()

template<typename QValue, typename Lock>

void bm::thread_pool< QValue, Lock >::stop | ( |  | ) |

inline noexcept

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.

## 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.

References is_stopped(), and stop_now.

## 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, bm::task_descr::func, is_stopped(), and stop_now.

Referenced by start().

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

- bmthreadpool.h