Tacopie  3.0.0
Tacopie is a TCP Client & Server C++11 library.
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
tacopie::utils::thread_pool Class Reference

#include <thread_pool.hpp>

Public Types

typedef std::function< void()> task_t
 

Public Member Functions

 thread_pool (std::size_t nb_threads)
 
 ~thread_pool (void)
 dtor More...
 
 thread_pool (const thread_pool &)=delete
 copy ctor More...
 
thread_pooloperator= (const thread_pool &)=delete
 assignment operator More...
 
void add_task (const task_t &task)
 
thread_pooloperator<< (const task_t &task)
 
void stop (void)
 
bool is_running (void) const
 
void set_nb_threads (std::size_t nb_threads)
 

Private Member Functions

void run (void)
 
task_t fetch_task (void)
 
bool should_stop (void) const
 

Private Attributes

std::vector< std::thread > m_workers
 
std::size_t m_nb_threads
 
std::atomic< bool > m_should_stop = ATOMIC_VAR_INIT(false)
 
std::queue< task_tm_tasks
 
std::mutex m_tasks_mtx
 
std::condition_variable m_tasks_condvar
 

Member Typedef Documentation

◆ task_t

typedef std::function<void()> tacopie::utils::thread_pool::task_t

task typedef simply a callable taking no parameter

Constructor & Destructor Documentation

◆ thread_pool() [1/2]

tacopie::utils::thread_pool::thread_pool ( std::size_t  nb_threads)
explicit

ctor created the worker thread that start working immediately

Parameters
nb_threadsnumber of threads to start the thread pool

◆ ~thread_pool()

tacopie::utils::thread_pool::~thread_pool ( void  )

dtor

◆ thread_pool() [2/2]

tacopie::utils::thread_pool::thread_pool ( const thread_pool )
delete

copy ctor

Member Function Documentation

◆ add_task()

void tacopie::utils::thread_pool::add_task ( const task_t task)

add tasks to thread pool task is enqueued and will be executed whenever all previously executed tasked have been executed (or are currently being executed)

Parameters
tasktask to be executed by the threadpool

◆ fetch_task()

task_t tacopie::utils::thread_pool::fetch_task ( void  )
private

retrieve a new task fetch the first element in the queue, or wait if no task are available may return null if the threadpool is stopped

◆ is_running()

bool tacopie::utils::thread_pool::is_running ( void  ) const
Returns
whether the thread_pool is running or not

◆ operator<<()

thread_pool& tacopie::utils::thread_pool::operator<< ( const task_t task)

same as add_task

Parameters
tasktask to be executed by the threadpool
Returns
current instance

◆ operator=()

thread_pool& tacopie::utils::thread_pool::operator= ( const thread_pool )
delete

assignment operator

◆ run()

void tacopie::utils::thread_pool::run ( void  )
private

worker main loop

◆ set_nb_threads()

void tacopie::utils::thread_pool::set_nb_threads ( std::size_t  nb_threads)

reset the number of threads working in the thread pool this can be safely called at runtime and can be useful if you need to adjust the number of workers

this function returns immediately, but change might be applied in the background that is, increasing number of threads will spwan new threads directly from this function (but they may take a while to start) moreover, shrinking the number of threads can only be applied in the background to make sure to not stop some threads in the middle of their task

changing number of workers do not affect tasks to be executed and tasks currently being executed

Parameters
nb_threadsnumber of threads

◆ should_stop()

bool tacopie::utils::thread_pool::should_stop ( void  ) const
private
Returns
whether the thread should stop or not

◆ stop()

void tacopie::utils::thread_pool::stop ( void  )

stop the thread pool and wait for workers completion if some tasks are pending, they won't be executed

Member Data Documentation

◆ m_nb_threads

std::size_t tacopie::utils::thread_pool::m_nb_threads
private

number of threads allowed

◆ m_should_stop

std::atomic<bool> tacopie::utils::thread_pool::m_should_stop = ATOMIC_VAR_INIT(false)
private

whether the thread_pool should stop or not

◆ m_tasks

std::queue<task_t> tacopie::utils::thread_pool::m_tasks
private

tasks

◆ m_tasks_condvar

std::condition_variable tacopie::utils::thread_pool::m_tasks_condvar
private

task condvar to sync on tasks changes

◆ m_tasks_mtx

std::mutex tacopie::utils::thread_pool::m_tasks_mtx
private

tasks thread safety

◆ m_workers

std::vector<std::thread> tacopie::utils::thread_pool::m_workers
private

threads


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