Tacopie  3.0.0
Tacopie is a TCP Client & Server C++11 library.
Public Types | Public Member Functions | 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
 
 thread_pool (const thread_pool &)=delete
 copy ctor
 
thread_pooloperator= (const thread_pool &)=delete
 assignment operator
 
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)
 

Detailed Description

basic thread pool used to push async tasks from the io_service

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

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

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

◆ 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

◆ 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

◆ 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


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