C++ Concurrency Sandbox
Loading...
Searching...
No Matches
ThreadPool Class Reference

Manages a collection of threads that execute tasks from a shared queue. More...

Collaboration diagram for ThreadPool:

Public Member Functions

 ThreadPool (size_t num_threads)
 Construct a new Thread Pool and spawns worker threads.
 
void enqueue (Task t)
 Submits a task to the queue for execution.
 
 ~ThreadPool ()
 Destructor that ensures all threads finish current work before exiting.
 
 ThreadPool (size_t num_threads)
 Constructs the pool and starts the worker threads.
 
void enqueue (Task t)
 Enqueues a Task (functor/lambda) into the work queue.
 
 ~ThreadPool ()
 Joins all threads for a clean shutdown.
 

Detailed Description

Manages a collection of threads that execute tasks from a shared queue.

A pool of persistent workers that execute generic Task objects.

Manages a set of persistent threads to execute fire-and-forget tasks.

  • The pool provides a mechanism to offload work to background threads and retrieve return values via std::future, preventing the main thread from blocking.
  • This implementation focuses on the "Fire-and-Forget" pattern, where tasks are submitted for execution without the caller expecting a return value.
  • This pool is oblivious to the return types of tasks; it simply executes them. Communication of results is handled externally by the tasks themselves.

Definition at line 36 of file BasicThreadPool.cpp.

Constructor & Destructor Documentation

◆ ThreadPool() [1/2]

ThreadPool::ThreadPool ( size_t  num_threads)
inline

Construct a new Thread Pool and spawns worker threads.

Parameters
num_threadsThe number of worker threads to maintain in the pool.

Definition at line 83 of file BasicThreadPool.cpp.

83 : stop(false) {
84 for (size_t i = 0; i < num_threads; ++i) {
85 workers.emplace_back(&ThreadPool::workerLoop, this);
86 }
87 }

◆ ~ThreadPool() [1/2]

ThreadPool::~ThreadPool ( )
inline

Destructor that ensures all threads finish current work before exiting.

  • Signals workers to stop, wakes everyone up, and joins all threads.

Definition at line 107 of file BasicThreadPool.cpp.

107 {
108 stop = true;
109 cv.notify_all();
110
111 for (std::thread &worker : workers) {
112 if (worker.joinable()) {
113 worker.join();
114 }
115 }
116 }

◆ ThreadPool() [2/2]

ThreadPool::ThreadPool ( size_t  num_threads)
inline

Constructs the pool and starts the worker threads.

Parameters
num_threadsNumber of threads to spawn.

Definition at line 104 of file MessagePassing.cpp.

104 : stop(false) {
105 for (size_t i = 0; i < num_threads; ++i) {
106 workers.emplace_back(&ThreadPool::workerLoop, this);
107 }
108 }

◆ ~ThreadPool() [2/2]

ThreadPool::~ThreadPool ( )
inline

Joins all threads for a clean shutdown.

Definition at line 125 of file MessagePassing.cpp.

125 {
126 stop = true;
127 cv.notify_all();
128 for (std::thread &worker : workers) {
129 if (worker.joinable()) worker.join();
130 }
131 }

Member Function Documentation

◆ enqueue() [1/2]

void ThreadPool::enqueue ( Task  t)
inline

Submits a task to the queue for execution.

  • This is a non-blocking call that moves the task into the internal queue and notifies a single worker thread.
    Parameters
    tThe task to execute (must match void() signature).

Definition at line 95 of file BasicThreadPool.cpp.

95 {
96 {
97 std::unique_lock<std::mutex> lock(queue_mtx);
98 tasks.push(std::move(t));
99 }
100 cv.notify_one();
101 }

Referenced by main().

Here is the caller graph for this function:

◆ enqueue() [2/2]

void ThreadPool::enqueue ( Task  t)
inline

Enqueues a Task (functor/lambda) into the work queue.

Parameters
tThe task to be executed by a worker.

Definition at line 114 of file MessagePassing.cpp.

114 {
115 {
116 std::unique_lock<std::mutex> lock(queue_mtx);
117 tasks.push(std::move(t));
118 }
119 cv.notify_one();
120 }

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