![]() |
Bitcoin Core 30.99.0
P2P Digital Currency
|
Fixed-size thread pool for running arbitrary tasks concurrently. More...
#include <threadpool.h>
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 |
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.
Start() and Stop() must be called from a controller (non-worker) thread. Calling Stop() from a worker thread will deadlock, as it waits for all workers to join, including the current one.Submit() can be called from any thread, including workers. It safely enqueues new work for execution as long as the pool has active workers.Interrupt() stops new task submission and lets queued ones drain in the background. Callers can continue other shutdown steps and call Stop() at the end to ensure no remaining tasks are left to execute.Stop() prevents further task submission and blocks until all the queued ones are completed. Definition at line 45 of file threadpool.h.
|
inlineexplicit |
Definition at line 88 of file threadpool.h.
|
inline |
|
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.
|
private |
|
inlineprivate |
Definition at line 55 of file threadpool.h.
|
private |
|
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.
|
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.
|
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.
|
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.
|
inline |
|
inlineprivate |
Definition at line 58 of file threadpool.h.
|
inline |
|
private |
Definition at line 51 of file threadpool.h.
|
private |
Definition at line 49 of file threadpool.h.
|
private |
Definition at line 48 of file threadpool.h.