![]() |
Bitcoin Core 30.99.0
P2P Digital Currency
|
Fixed-size thread pool for running arbitrary tasks concurrently. More...
#include <threadpool.h>
Public Types | |
| enum class | SubmitError { Inactive , Interrupted } |
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 > | |
| util::Expected< std::future< std::invoke_result_t< F > >, SubmitError > | Submit (F &&fn) noexcept EXCLUSIVE_LOCKS_REQUIRED(!m_mutex) |
| Enqueues a new task for asynchronous execution. More... | |
| bool | 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 46 of file threadpool.h.
|
strong |
| Enumerator | |
|---|---|
| Inactive | |
| Interrupted | |
Definition at line 154 of file threadpool.h.
|
inlineexplicit |
Definition at line 89 of file threadpool.h.
|
inline |
|
private |
|
inlineprivate |
Definition at line 56 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.
Note: The next step in the pool lifecycle is calling Stop(), which releases any dangling resources and resets the pool state for shutdown or restart.
Definition at line 218 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 193 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 104 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. Concurrent calls to Start() will be rejected while Stop() is in progress.
Definition at line 127 of file threadpool.h.
|
inlinenoexcept |
Enqueues a new task for asynchronous execution.
| fn | Callable to execute asynchronously. |
Thread-safe: Can be called from any thread, including within the provided 'fn' callable.
Definition at line 174 of file threadpool.h.
|
inline |
|
inlineprivate |
|
inline |
|
private |
Definition at line 52 of file threadpool.h.
|
private |
Definition at line 50 of file threadpool.h.
|
private |
Definition at line 49 of file threadpool.h.