GCSIM
Public Member Functions | Private Member Functions | Private Attributes | List of all members
thread_pool Class Reference

manages a fixed number of worker threads that execute queued tasks. More...

#include <thread-pool.hpp>

Public Member Functions

 thread_pool (size_t thread_count)
 starts specified number of worker threads.
 
 ~thread_pool ()
 stopping all worker threads.
 
template<typename T >
void enqueue (T &&f)
 adding new task to the queue
 

Private Member Functions

void worker ()
 threads execute tasks from the queue.
 

Private Attributes

std::mutex mtx
 mutex for concurrent access to tasks.
 
std::condition_variable cv
 condition variable for waiting/notifying.
 
bool stop
 flag that handles stoppage of worker threads.
 
queue< std::function< void()> > tasks
 queue of tasks.
 
std::thread * threads
 list of threads owned by thread pool.
 
size_t thread_count
 number of threads in a thread pool.
 

Detailed Description

manages a fixed number of worker threads that execute queued tasks.

Constructor & Destructor Documentation

◆ thread_pool()

thread_pool::thread_pool ( size_t  thread_count)

starts specified number of worker threads.

Parameters
thread_count- number of worker threads that handle tasks.

◆ ~thread_pool()

thread_pool::~thread_pool ( )

stopping all worker threads.

Member Function Documentation

◆ enqueue()

template<typename T >
void thread_pool::enqueue ( T &&  f)
inline

adding new task to the queue

Parameters
f- task that is being added to queue.
Exceptions
std::runtime_error- when called after all worker threads stopped working.

◆ worker()

void thread_pool::worker ( )
private

threads execute tasks from the queue.

Member Data Documentation

◆ cv

std::condition_variable thread_pool::cv
private

condition variable for waiting/notifying.

◆ mtx

std::mutex thread_pool::mtx
private

mutex for concurrent access to tasks.

◆ stop

bool thread_pool::stop
private

flag that handles stoppage of worker threads.

◆ tasks

queue<std::function<void()> > thread_pool::tasks
private

queue of tasks.

◆ thread_count

size_t thread_pool::thread_count
private

number of threads in a thread pool.

◆ threads

std::thread* thread_pool::threads
private

list of threads owned by thread pool.


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