-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
62 lines (38 loc) · 3.01 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
ThreadPool Class (REQUIRES C++17)
The ThreadPool class is a C++ class that provides a simple thread pool implementation. A thread pool is a group of worker threads that are created at initialization time and are used to execute a queue of tasks. This can help improve the performance of programs that need to perform many small, independent tasks, by minimizing the overhead associated with thread creation and destruction.
Usage
To use the ThreadPool class in your program, you first need to include the appropriate header file:
#include "thread_pool.hpp"
You can then create a ThreadPool object by specifying the number of threads to use:
ThreadPool pool(4); // create a thread pool with 4 worker threads
To add a task to the thread pool, you can use the enqueue() member function:
pool.enqueue([](){
// code to execute in the worker thread
});
The argument to enqueue() is a callable object (e.g., a lambda function) that will be executed in one of the worker threads.
The enqueue() function returns a std::future object that can be used to retrieve the result of the task when it completes:
auto result = pool.enqueue_result([](){
// code to execute in the worker thread
return 42;
});
// wait for the task to complete and retrieve the result
int answer = result.get();
You can add as many tasks to the thread pool as you like. They will be executed in the order in which they are added, as worker threads become available.
Thread Safety
The ThreadPool class is designed to be thread-safe, meaning that it can be used from multiple threads concurrently without causing race conditions or other synchronization problems. However, it is the responsibility of the user to ensure that any shared data structures or resources accessed by the tasks are also thread-safe.
Implementation
The ThreadPool class is implemented using C++11 features, including std::thread, std::mutex, std::condition_variable, and std::future. It uses a std::vector to store the worker threads and a std::queue to store the tasks. A std::condition_variable is used to signal worker threads when a new task is added to the queue.
The ThreadPool class uses RAII (Resource Acquisition Is Initialization) to ensure that all threads are properly cleaned up when the object is destroyed. The threads are joined in the destructor, which waits for all outstanding tasks to complete before destroying the threads.
Limitations
The ThreadPool class has some limitations that you should be aware of:
The tasks executed by the worker threads must be independent and not share any resources, as there is no guarantee of thread safety.
There is no mechanism for canceling tasks once they have been added to the queue.
The number of worker threads is fixed at initialization time and cannot be changed dynamically.
The task queue is unbounded, so adding too many tasks can potentially consume all available memory.
How to build:
- Go to the repo's root folder
- mkdir build
- cd build
- cmake ..
- make
- execute the main file with ./main