-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads.h
106 lines (100 loc) · 2.33 KB
/
threads.h
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
105
106
#include <queue>
#include <thread>
#include <condition_variable>
#include <mutex>
#include <vector>
#include <iostream>
#include <functional>
#include <future>
enum class operator_value
{
operator_success,
operator_failure
};
class task
{
public:
task(){};
template <typename F, typename... Args>
auto add_task(F&& f, Args&& ...args)
{
std::function<decltype(f(args...))()> func = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
auto task_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);
svc = std::function([task_ptr]{
(*task_ptr)();
});
return task_ptr->get_future();
}
void process()
{
svc();
}
private:
std::function<void()> svc;
};
// template <typename F, typename... args>
class thread_pool
{
public:
thread_pool()
{
thread_num_ = 5;
stop = false;
};
thread_pool(int& num)
{
thread_num_ = num;
stop = false;
}
void vreat_threads(int& num)
{
for(auto i = 0; i < thread_num_; i++)
{
thread_array.push_back(std::thread([&]{sing_thread();}));
}
}
void sing_thread()
{
// optimize the lock scope
while(!stop)
{
std::unique_lock<std::mutex>lock(mutex_);
while(task_queue_.empty())
{
semaphore.wait(lock, [&]{return task_queue_.empty();});
}
auto cur_task = std::move(task_queue_.front());
task_queue_.pop();
cur_task.process();
semaphore.notify_one();
}
}
auto thread_stop()
{
stop = true;
semaphore.notify_all();
for(auto& thread:thread_array)
{
thread.join();
}
return operator_value::operator_success;
}
auto thread_start()
{
vreat_threads(thread_num_);
}
auto push_task(task&& cur_task )
{
std::unique_lock<std::mutex>lock(mutex_);
task_queue_.push(std::move(cur_task));
semaphore.notify_one();
return operator_value::operator_success;
}
private:
std::atomic<bool> stop;
std::mutex mutex_;
std::condition_variable semaphore;
int thread_num_;
std::queue<task>task_queue_;
std::vector<std::thread>thread_array;
};