Bitcoin Core 30.99.0
P2P Digital Currency
Public Member Functions | Private Member Functions | Private Attributes | List of all members
ThreadPool Class Reference

Fixed-size thread pool for running arbitrary tasks concurrently. More...

#include <threadpool.h>

Collaboration diagram for ThreadPool:
[legend]

Public Member Functions

 ThreadPool (const std::string &name)
 
 ~ThreadPool ()
 
void Start (int num_workers) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
 Start worker threads. More...
 
void Stop () EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
 Stop all worker threads and wait for them to exit. More...
 
template<class F >
 EXCLUSIVE_LOCKS_REQUIRED (!m_mutex) auto Submit(F &&fn)
 Enqueues a new task for asynchronous execution. More...
 
void ProcessTask () EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
 Execute a single queued task synchronously. More...
 
void Interrupt () EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
 Stop accepting new tasks and begin asynchronous shutdown. More...
 
size_t WorkQueueSize () EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
 
size_t WorkersCount () EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
 

Private Member Functions

std::queue< std::packaged_task< void()> > m_work_queue GUARDED_BY (m_mutex)
 
bool m_interrupt GUARDED_BY (m_mutex)
 
std::vector< std::thread > m_workers GUARDED_BY (m_mutex)
 
void WorkerThread () EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
 

Private Attributes

std::string m_name
 
Mutex m_mutex
 
std::condition_variable m_cv
 

Detailed Description

Fixed-size thread pool for running arbitrary tasks concurrently.

The thread pool maintains a set of worker threads that consume and execute tasks submitted through Submit(). Once started, tasks can be queued and processed asynchronously until Stop() is called.

Thread-safety and lifecycle

Definition at line 45 of file threadpool.h.

Constructor & Destructor Documentation

◆ ThreadPool()

ThreadPool::ThreadPool ( const std::string &  name)
inlineexplicit

Definition at line 88 of file threadpool.h.

◆ ~ThreadPool()

ThreadPool::~ThreadPool ( )
inline

Definition at line 90 of file threadpool.h.

Here is the call graph for this function:

Member Function Documentation

◆ EXCLUSIVE_LOCKS_REQUIRED()

template<class F >
ThreadPool::EXCLUSIVE_LOCKS_REQUIRED ( m_mutex) &&
inline

Enqueues a new task for asynchronous execution.

Returns a std::future that provides the task's result or propagates any exception it throws. Note: Ignoring the returned future requires guarding the task against uncaught exceptions, as they would otherwise be silently discarded.

Definition at line 154 of file threadpool.h.

Here is the caller graph for this function:

◆ GUARDED_BY() [1/3]

std::queue< std::packaged_task< void()> > m_work_queue ThreadPool::GUARDED_BY ( m_mutex  )
private

◆ GUARDED_BY() [2/3]

bool m_interrupt ThreadPool::GUARDED_BY ( m_mutex  )
inlineprivate

Definition at line 55 of file threadpool.h.

◆ GUARDED_BY() [3/3]

std::vector< std::thread > m_workers ThreadPool::GUARDED_BY ( m_mutex  )
private

◆ Interrupt()

void ThreadPool::Interrupt ( )
inline

Stop accepting new tasks and begin asynchronous shutdown.

Wakes all worker threads so they can drain the queue and exit. Unlike Stop(), this function does not wait for threads to finish.

Definition at line 194 of file threadpool.h.

Here is the caller graph for this function:

◆ ProcessTask()

void ThreadPool::ProcessTask ( )
inline

Execute a single queued task synchronously.

Removes one task from the queue and executes it on the calling thread.

Definition at line 174 of file threadpool.h.

Here is the caller graph for this function:

◆ Start()

void ThreadPool::Start ( int  num_workers)
inline

Start worker threads.

Creates and launches num_workers threads that begin executing tasks from the queue. If the pool is already started, throws.

Must be called from a controller (non-worker) thread.

Definition at line 103 of file threadpool.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Stop()

void ThreadPool::Stop ( )
inline

Stop all worker threads and wait for them to exit.

Sets the interrupt flag, wakes all waiting workers, and joins them. Any remaining tasks in the queue will be processed before returning.

Must be called from a controller (non-worker) thread.

Definition at line 125 of file threadpool.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ WorkersCount()

size_t ThreadPool::WorkersCount ( )
inline

Definition at line 205 of file threadpool.h.

Here is the caller graph for this function:

◆ WorkerThread()

void ThreadPool::WorkerThread ( )
inlineprivate

Definition at line 58 of file threadpool.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ WorkQueueSize()

size_t ThreadPool::WorkQueueSize ( )
inline

Definition at line 200 of file threadpool.h.

Here is the caller graph for this function:

Member Data Documentation

◆ m_cv

std::condition_variable ThreadPool::m_cv
private

Definition at line 51 of file threadpool.h.

◆ m_mutex

Mutex ThreadPool::m_mutex
private

Definition at line 49 of file threadpool.h.

◆ m_name

std::string ThreadPool::m_name
private

Definition at line 48 of file threadpool.h.


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