Definition of C++ Thread Pool
Threadpool in C++ is basically a pool having a fixed number of threads used when we want to work multiple tasks together (run multiple threads concurrently). This thread sits idle in the thread pool when there are no tasks and when a task arrives, it is sent to the thread pool and gets assigned to the thread. Pending tasks will remain in the queue waiting for a thread to get free. In C++ there is no specific library for thread pool, but it provides various methods in the API which the programmer can use and create the one according to the requirements.
Syntax:
Below given is the basic syntax of using the C++ thread pool:
using namespace std;
// initializing the number of threads
int count_threads = thread: : hardware_concurrency();
int main()
{
thread_pool pool;
// queuing the work/ task to thread pool queue and store the future
auto res = pool.enqueue([](int x) { return x; }, 42);
//printing the output on console
cout << res.get() << endl;
}
In the above syntax,
- thread: hardware_concurrency(): It basically initializes the fixed number of threads that are going to work on the desired task.
- Pool.enqueue: It will enqueue the task request in the pool which needs to be processed.
- res.get(): It is used to get the result from the future.
How Thread Pools Works in C++?
It is nice to work with the thread pools when we have a large number of tasks and we want them to execute parallelly. It reduces the overhead of the creation of threads every time and too much of threads reduce the overall speed of the system. There is no specific library in C++ which provides the thread pool so the programmer has to create it is own depending on the requirements.
Below given is the step by step procedure of the working of thread in the thread pool in C++ :
1. Threadpool class is initialized with some fixed number of worker threads which can be done by thread::hardware_concurrency() function. Initially we can also create zero (0) threads when there is no work to be done. These created threads wait on the condition variable.
2. When a new task arrives, it is first put in the queue of pending work, and the following steps are performed:
- It is first checked whether the thread in the thread pool is free or not.
- If the thread is free, it is woken up by signaling the condition variable. It then takes off the work from the task queue to perform the desired task. Once the task is completed, it then goes back to that queue for more work.
- Once the thread performs the desired task, the thread again waits on the condition variable to show its state.
3. In a thread pool, objects are destroyed only when the destructor of thread pool class is called by the programmer.
Examples
One can implement the thread pool using the C++ APIs according to the requirements. But there are various challenges that the user/programmer faces while designing it. Some of them can be a number of threads that should be created in order to have efficient performance, techniques to be used to allocate the task to the thread, whether the waiting for the completion of tasks should be done or not, waiting for task queue techniques, etc.
One of the simplest implementations of the thread pool is given below:
#include <iostream>
#include <cstdlib>
#include <thread>
#include create_pool.h
using namespace std;
#define trace(x)
// function to define the number of threads
auto int num_threads = thread::hardware_concurrency();
//checking condition of the number of threads available
// if the number of threads is 0
if(num_threads == 0)
{
num_threads =1;
}
const int total_task = 20000;
//main logic inside the main function with 2 arguments
int main(int argc, char** argv)
{
srand((unsigned int)time(NULL));
//creating a thread pool
thread_pool p;
//enqueue function used to queue the request that needs to be processed by
//thread pool and retrieving the output in 'output' variable
auto output = p.enqueue_task([](int x) { return x; }, 0xFF);
output.get();
//processing the request
for(int x = 1; x <= num_threads; x++)
p.enqueue_work([](int thread_number) {
int work_out = 0;
int work = total_task + (rand() % (total_task));
trace("Thread " << thread_number << "is going to start " << work );
for(int y = 0; y < work; y++)
work_out += rand();
trace("Execution of " << thread_number << " is ended");
}, x);
return 1;
}
Explanation:
In the above code, firstly the threads are created using ‘thread::hardware_concurrency()’ function and checked if the number of threads is 0, then it makes 1 active thread available. Function ‘enqueue_task() is used to queue the task that needs to be processed in the pool. The results of them are stored in the output variable. In order to retrieve the exact data from it, the get() function is used. Finally, the processing of thread requests is done using each request at a time in the ‘for’ loop and there in between processing and end is displayed on the console to make it easy to understand the flow.
This is the basic C++ code providing a glimpse of how the thread pool is implemented in a normal C++ application. One needs to implement a lot of things while working in the real application checking various conditions. One can also add the mutex, which is listening from another application like database, disk, etc.
Moreover, create_pool.h file included in the above code needs to be created according to the requirements performing the basic functions, like creating a pool using the blocking_queue method, destruction of threads, etc.
One needs to be at the expert level and understand all the in-depth working of threads and the basic functions of C++ and their implementation before working on it.
Conclusion
The above description clearly explains what is C++ thread pool is and how it can be used in a program. Thread pools are important to use in an application as it becomes easy and efficient to use the threads. Moreover, it does not keep the system overloaded as the user/ programmer can limit the number of threads and the tasks assigned to them. Tasks will remain in the queue and will execute once the thread becomes free.
Recommended Articles
This is a guide to C++ Thread Pool. Here we discuss the Definition of C++ Thread Pool with How Thread Pools Work in C++ with programming examples?. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses