-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.cpp
More file actions
104 lines (85 loc) · 2.67 KB
/
threadpool.cpp
File metadata and controls
104 lines (85 loc) · 2.67 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <vector>
#include <queue>
#include <future>
class threadpool{
public:
explicit threadpool(int n = 1): n_threds(n), stop(false){
for (int i=0; i<n; i++){
threads.emplace_back([this](){
//we'll be reading from the queue
while(1){
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [this](){
return !tasks.empty() || stop;
});
if (stop){
return ;
}
if (tasks.empty()){
continue;
}
std::function<void()> task = std::move(tasks.front());
tasks.pop();
//safe to unlock
lock.unlock();
//execute task
task();
}
});
}
}
//we can use perfect forwarding
template <typename F, typename... Args>
auto execute_task(F&& f, Args&&... args) -> std::future<decltype(f(args...))>{
using return_type = decltype(f(args...));
auto task = std::make_shared<std::packaged_task<return_type()>>(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
std::future<return_type> res = task->get_future();
std::unique_lock<std::mutex> lock(mtx);
tasks.emplace([task]() -> void{
(*task)();
});
lock.unlock();
cv.notify_one();
return res;
}
~threadpool(){
std::unique_lock<std::mutex> lock(mtx);
stop = true;
lock.unlock();
cv.notify_all();
for (auto &task: threads){
task.join();
}
}
private:
const int n_threds; //will keep this fixed !!
std::mutex mtx; //mutex to prevent race conditions in the tasks queue
std::condition_variable cv;
std::queue<std::function<void()>> tasks;
std::vector<std::thread> threads;
bool stop;
};
int fun(int n, int m){
std::this_thread::sleep_for(std::chrono::seconds(30));
std::cout<<"function executed \n";
return n*m;
}
int main(){
threadpool pool(4);
auto v = pool.execute_task(fun,4,4);
std::cout<<"return value "<<v.get()<<"\n";
// std::cout<<"threadpool program \n";
// for (int i=0; i<1000; i++){
// std::thread t1(fun);
// t1.detach();
// }
std::this_thread::sleep_for(std::chrono::seconds(100));
}