MySQL 26.7.0
Source Code Documentation
mysql::scheduler::Thread_pool< T, t_queue_size > Class Template Reference

MySQL wrapper for a condition variable, template which may be specialized with a specific implementation of a condition variable, e.g. More...

#include <thread_pool.h>

Public Types

using Thread = concurrency::Thread
 Thread type. More...
 
using Task_type = std::packaged_task< T(unsigned int)>
 Runnable task type, future is obtained by the caller. More...
 
using Queue_type = concurrency::Sync_bounded_queue< Task_type, t_queue_size >
 Queue type used to synchronize a thread supplying task to this thread pool and worker threads. More...
 
using St_key = mysql::concurrency::Stage_key
 
using Th_key = mysql::concurrency::Thread_key
 
using Thread_allocator = mysql::allocators::Allocator< Thread >
 

Public Member Functions

 Thread_pool (unsigned int thread_num=std::thread::hardware_concurrency(), unsigned int instance_id=0, Thread_pool_psi psi_params={})
 Constructs a thread pool. More...
 
bool init ()
 Initializes the thread pool by starting worker threads. More...
 
void deinit ()
 Deinitializes the thread pool and joins all worker threads. More...
 
void enqueue (Task_type &&task)
 Enqueues task for execution. More...
 
virtual ~Thread_pool ()
 Destructor. More...
 
void end_execution ()
 Notifies all threads to end execution in a gracious way. More...
 
 Thread_pool (Thread_pool &&) noexcept=delete
 
Thread_pooloperator= (Thread_pool &&) noexcept=delete
 
 Thread_pool (const Thread_pool &)=delete
 
Thread_pooloperator= (const Thread_pool &)=delete
 
std::string print_queue_state () const
 Prints worker queue state, debug function. More...
 
std::size_t queue_size () const
 Get an estimation of number of elements in the worker queue. More...
 
std::size_t size () const
 Obtains worker pool size. More...
 

Private Member Functions

void run_worker (unsigned int thread_id)
 Function executed by each thread. More...
 

Static Private Member Functions

static bool has_creation_error (const Thread &thread)
 Checks whether worker thread construction failed. More...
 

Private Attributes

std::vector< Thread, Thread_allocatorm_workers
 Thread container. More...
 
std::unordered_map< unsigned int, std::atomic< bool > > m_end_execution
 Variable indicating the end of execution, allows threads to stop in a gracious way. More...
 
Queue_type m_tasks
 Enqueued, ready to be executed tasks. More...
 
unsigned int m_workers_num
 The number of threads in the thread pool. More...
 
unsigned int m_instance_id {0}
 Instance id. More...
 
Thread_pool_psi m_psi
 PSI parameters. More...
 
bool m_initialized {false}
 Whether init() completed successfully. More...
 

Detailed Description

template<class T, std::size_t t_queue_size = 8192>
class mysql::scheduler::Thread_pool< T, t_queue_size >

MySQL wrapper for a condition variable, template which may be specialized with a specific implementation of a condition variable, e.g.

MySQL condition variable, and satisfying the following requirements:

Member Typedef Documentation

◆ Queue_type

template<class T , std::size_t t_queue_size = 8192>
using mysql::scheduler::Thread_pool< T, t_queue_size >::Queue_type = concurrency::Sync_bounded_queue<Task_type, t_queue_size>

Queue type used to synchronize a thread supplying task to this thread pool and worker threads.

Worker threads are consumers of this queue type

◆ St_key

template<class T , std::size_t t_queue_size = 8192>
using mysql::scheduler::Thread_pool< T, t_queue_size >::St_key = mysql::concurrency::Stage_key

◆ Task_type

template<class T , std::size_t t_queue_size = 8192>
using mysql::scheduler::Thread_pool< T, t_queue_size >::Task_type = std::packaged_task<T(unsigned int)>

Runnable task type, future is obtained by the caller.

◆ Th_key

template<class T , std::size_t t_queue_size = 8192>
using mysql::scheduler::Thread_pool< T, t_queue_size >::Th_key = mysql::concurrency::Thread_key

◆ Thread

template<class T , std::size_t t_queue_size = 8192>
using mysql::scheduler::Thread_pool< T, t_queue_size >::Thread = concurrency::Thread

Thread type.

◆ Thread_allocator

template<class T , std::size_t t_queue_size = 8192>
using mysql::scheduler::Thread_pool< T, t_queue_size >::Thread_allocator = mysql::allocators::Allocator<Thread>

Constructor & Destructor Documentation

◆ Thread_pool() [1/3]

template<class T , std::size_t t_queue_size>
mysql::scheduler::Thread_pool< T, t_queue_size >::Thread_pool ( unsigned int  thread_num = std::thread::hardware_concurrency(),
unsigned int  instance_id = 0,
Thread_pool_psi  psi_params = {} 
)

Constructs a thread pool.

Parameters
thread_numNumbers of threads in a thread pool, set as default to the number of concurrent threads supported by the implementation
instance_idTHP will gather statistics for this instance id
psi_paramsPSI parameters

◆ ~Thread_pool()

template<class T , std::size_t t_queue_size>
mysql::scheduler::Thread_pool< T, t_queue_size >::~Thread_pool
virtual

Destructor.

◆ Thread_pool() [2/3]

template<class T , std::size_t t_queue_size = 8192>
mysql::scheduler::Thread_pool< T, t_queue_size >::Thread_pool ( Thread_pool< T, t_queue_size > &&  )
deletenoexcept

◆ Thread_pool() [3/3]

template<class T , std::size_t t_queue_size = 8192>
mysql::scheduler::Thread_pool< T, t_queue_size >::Thread_pool ( const Thread_pool< T, t_queue_size > &  )
delete

Member Function Documentation

◆ deinit()

template<class T , std::size_t t_queue_size>
void mysql::scheduler::Thread_pool< T, t_queue_size >::deinit

Deinitializes the thread pool and joins all worker threads.

◆ end_execution()

template<class T , std::size_t t_queue_size>
void mysql::scheduler::Thread_pool< T, t_queue_size >::end_execution

Notifies all threads to end execution in a gracious way.

When finished, Thread_pool object is ready to be destroyed. This function is called in destructor.

◆ enqueue()

template<class T , std::size_t t_queue_size>
void mysql::scheduler::Thread_pool< T, t_queue_size >::enqueue ( Task_type &&  task)

Enqueues task for execution.

Parameters
taskFunctor to be executed by the thread pool

◆ has_creation_error()

template<class T , std::size_t t_queue_size>
bool mysql::scheduler::Thread_pool< T, t_queue_size >::has_creation_error ( const Thread thread)
staticprivate

Checks whether worker thread construction failed.

Parameters
threadWorker thread object.
Returns
True on failure, false otherwise.

◆ init()

template<class T , std::size_t t_queue_size>
bool mysql::scheduler::Thread_pool< T, t_queue_size >::init

Initializes the thread pool by starting worker threads.

Return values
falseSuccess
trueFailure

◆ operator=() [1/2]

template<class T , std::size_t t_queue_size = 8192>
Thread_pool & mysql::scheduler::Thread_pool< T, t_queue_size >::operator= ( const Thread_pool< T, t_queue_size > &  )
delete

◆ operator=() [2/2]

template<class T , std::size_t t_queue_size = 8192>
Thread_pool & mysql::scheduler::Thread_pool< T, t_queue_size >::operator= ( Thread_pool< T, t_queue_size > &&  )
deletenoexcept

◆ print_queue_state()

template<class T , std::size_t t_queue_size>
std::string mysql::scheduler::Thread_pool< T, t_queue_size >::print_queue_state

Prints worker queue state, debug function.

Returns
String with internal queue state

◆ queue_size()

template<class T , std::size_t t_queue_size>
std::size_t mysql::scheduler::Thread_pool< T, t_queue_size >::queue_size

Get an estimation of number of elements in the worker queue.

Returns
The number of elements in the worker queue

◆ run_worker()

template<class T , std::size_t t_queue_size>
void mysql::scheduler::Thread_pool< T, t_queue_size >::run_worker ( unsigned int  thread_id)
private

Function executed by each thread.

◆ size()

template<class T , std::size_t t_queue_size>
std::size_t mysql::scheduler::Thread_pool< T, t_queue_size >::size

Obtains worker pool size.

Returns
The number of workers available in the pool

Member Data Documentation

◆ m_end_execution

template<class T , std::size_t t_queue_size = 8192>
std::unordered_map<unsigned int, std::atomic<bool> > mysql::scheduler::Thread_pool< T, t_queue_size >::m_end_execution
private

Variable indicating the end of execution, allows threads to stop in a gracious way.

◆ m_initialized

template<class T , std::size_t t_queue_size = 8192>
bool mysql::scheduler::Thread_pool< T, t_queue_size >::m_initialized {false}
private

Whether init() completed successfully.

◆ m_instance_id

template<class T , std::size_t t_queue_size = 8192>
unsigned int mysql::scheduler::Thread_pool< T, t_queue_size >::m_instance_id {0}
private

Instance id.

◆ m_psi

template<class T , std::size_t t_queue_size = 8192>
Thread_pool_psi mysql::scheduler::Thread_pool< T, t_queue_size >::m_psi
private

PSI parameters.

◆ m_tasks

template<class T , std::size_t t_queue_size = 8192>
Queue_type mysql::scheduler::Thread_pool< T, t_queue_size >::m_tasks
private

Enqueued, ready to be executed tasks.

◆ m_workers

template<class T , std::size_t t_queue_size = 8192>
std::vector<Thread, Thread_allocator> mysql::scheduler::Thread_pool< T, t_queue_size >::m_workers
private

Thread container.

◆ m_workers_num

template<class T , std::size_t t_queue_size = 8192>
unsigned int mysql::scheduler::Thread_pool< T, t_queue_size >::m_workers_num
private

The number of threads in the thread pool.


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