OpenJPH: ojph::thds::thread_pool Class Reference

OpenJPH Open-source implementation of JPEG2000 Part-15

Loading...
Searching...
No Matches
ojph::thds::thread_pool Class Reference

Implements a pool of threads, and can queue tasks. More...

`#include <ojph_threads.h>`

## Public Member Functions
| thread_pool ()
| default constructor
| ~thread_pool ()
| default destructor
void | init (size_t num_threads)
| Initializes the thread pool.
void | add_task (worker_thread_base *task)
| Adds a task to the thread pool.
size_t | get_num_threads ()
| Returns the number of threads in the thread pool.

## Static Private Member Functions
static void | start_thread (thread_pool *tp)
| A static function to start a thread.

## Private Attributes
std::vector< std::thread > | threads
std::deque< worker_thread_base * > | tasks
std::mutex | mutex
std::condition_variable | condition
std::atomic_bool | stop

## Detailed Description

Implements a pool of threads, and can queue tasks.

Definition at line 97 of file ojph_threads.h.

## Constructor & Destructor Documentation

## ◆ thread_pool()

ojph::thds::thread_pool::thread_pool | ( |  | ) |
| inline

default constructor

Definition at line 103 of file ojph_threads.h.

References stop.

Referenced by start_thread().

## ◆ ~thread_pool()

ojph::thds::thread_pool::~thread_pool | ( |  | ) |

default destructor

Definition at line 54 of file ojph_threads.cpp.

References condition, stop, and threads.

## Member Function Documentation

## ◆ add_task()

void ojph::thds::thread_pool::add_task | ( | worker_thread_base * | task | ) |

Adds a task to the thread pool.

Parameters

  task | the task to added, must be derived from worker_thread_base

Definition at line 73 of file ojph_threads.cpp.

References condition, mutex, and tasks.

## ◆ get_num_threads()

size_t ojph::thds::thread_pool::get_num_threads | ( |  | ) |
| inline

Returns the number of threads in the thread pool.

@retuen number of threads in the thread pool

Definition at line 129 of file ojph_threads.h.

References threads.

## ◆ init()

void ojph::thds::thread_pool::init | ( | size_t | num_threads | ) |

Initializes the thread pool.

Parameters

  num_threads | the number of threads the thread pool holds

Definition at line 63 of file ojph_threads.cpp.

References start_thread(), and threads.

Referenced by main().

## ◆ start_thread()

void ojph::thds::thread_pool::start_thread | ( | thread_pool * | tp | ) |
| staticprivate

A static function to start a thread.

Parameters

  tp | a pointer to the thread pool

Definition at line 82 of file ojph_threads.cpp.

References condition, ojph::thds::worker_thread_base::execute(), mutex, stop, tasks, and thread_pool().

Referenced by init().

## Member Data Documentation

## ◆ condition

std::condition_variable ojph::thds::thread_pool::condition
| private

Definition at line 143 of file ojph_threads.h.

Referenced by add_task(), start_thread(), and ~thread_pool().

## ◆ mutex

std::mutex ojph::thds::thread_pool::mutex
| private

Definition at line 142 of file ojph_threads.h.

Referenced by add_task(), and start_thread().

## ◆ stop

std::atomic_bool ojph::thds::thread_pool::stop
| private

Definition at line 144 of file ojph_threads.h.

Referenced by start_thread(), thread_pool(), and ~thread_pool().

## ◆ tasks

std::deque<worker_thread_base*> ojph::thds::thread_pool::tasks
| private

Definition at line 141 of file ojph_threads.h.

Referenced by add_task(), and start_thread().

## ◆ threads

std::vector<std::thread> ojph::thds::thread_pool::threads
| private

Definition at line 140 of file ojph_threads.h.

Referenced by get_num_threads(), init(), and ~thread_pool().

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

- /startdir/src/OpenJPH/src/apps/common/ojph_threads.h
- /startdir/src/OpenJPH/src/apps/others/ojph_threads.cpp