-
Notifications
You must be signed in to change notification settings - Fork 1
/
SmartTP.h
172 lines (135 loc) · 3.71 KB
/
SmartTP.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#pragma once
#ifndef _SMART_TP_H
#define _SMART_TP_H
#include <functional>
#include <future>
#include <mutex>
#include <queue>
#include <thread>
#include <utility>
#include <vector>
#include <iostream>
#include "MutexQueue.h"
using namespace std;
class SmartTP{
private:
enum class ThreadStates {waiting = 1, running = 2, stopped = 3};
enum class ThreadTypes {core = 1, extra = 2};
//using ThreadPtr = shared_ptr<thread>;
//using ThreadId = atomic<int>;
/*
struct SmartThread{
thread* _t;
ThreadId _id;
ThreadState _state;
ThreadType _type;
SmartThread(){
_t = nullptr;
_id = 0;
_state.store(ThreadStates::waiting);
}
};*/
//using SmartThreadPtr = shared_ptr<SmartThread>;
class Worker{
private:
int worker_id;
SmartTP* m_tp; //the thread pool it belongs to.
public:
Worker(SmartTP* tp, const int id) : m_tp(tp), worker_id(id){}
void operator()(){
function<void()> func;
bool dequeued;
while(!m_tp->m_isClosed){
{
unique_lock<mutex> lock(m_tp->m_cond_mutex);
if(m_tp->m_queue.isEmpty()){
m_tp->m_cond_lock.wait(lock);
}
dequeued = m_tp->m_queue.dequeue(func);
}
if(dequeued){
m_tp->m_states[worker_id].store(ThreadStates::running);
func();
m_tp->checkSysStatus();
}
}
}
};
bool m_isClosed;
MutexQueue<function<void()>> m_queue;
vector<thread> m_threads;
vector<atomic<ThreadTypes>> m_types;
vector<atomic<ThreadStates>> m_states;
int m_current_thread_no;
mutex m_cond_mutex, m_sys_check_mutex;
condition_variable m_cond_lock;
public:
SmartTP(const int default_thread_n, const int max_thread_n) : m_threads(vector<thread>(max_thread_n)),
m_states(vector<atomic<ThreadStates>>(max_thread_n)),
m_types(vector<atomic<ThreadTypes>>(max_thread_n)),
m_current_thread_no(default_thread_n),
m_isClosed(false){}
SmartTP(const SmartTP &) = delete;
SmartTP(const SmartTP &&) = delete;
SmartTP & operator=(const SmartTP &) = delete;
SmartTP & operator=(SmartTP &&) = delete;
void init();
void close();
void checkSysStatus();
template<typename F, typename...Args>
auto submit(F&& f, Args&&... args) -> future<decltype(f(args...))>{
function<decltype(f(args...))()> tmpFunc = bind(forward<F>(f), forward<Args>(args)...);
auto task_ptr = make_shared<packaged_task<decltype(f(args...))()>>(tmpFunc);
function<void()> wrapper_func = [task_ptr](){
(*task_ptr)();
};
m_queue.enqueue(wrapper_func);
m_cond_lock.notify_one();
return task_ptr->get_future();
}
};
/**
* A number n of threads is created in the thread pool,
* waiting to be called on execution.
*/
void SmartTP::init(){
for(int i = 0; i < m_current_thread_no; ++i){
m_threads[i] = thread(Worker(this, i));
m_types[i].store(ThreadTypes::core);
m_states[i].store(ThreadStates::waiting);
}
}
/**
* Close the whole thread pool upon release all resources.
*/
void SmartTP::close(){
while(m_queue.size() > 0);
m_isClosed = true;
m_cond_lock.notify_all();
for(int i = 0; i < m_threads.size(); ++i){
if(m_threads[i].joinable()){
m_threads[i].join();
}
}
//cout << static_cast<int>(m_current_thread_no) << endl;
}
void SmartTP::checkSysStatus(){
unique_lock<mutex> lock(m_sys_check_mutex);
bool tmp = true;
if(m_current_thread_no >= m_threads.size()) return;
//cout << "checking sys status" << endl;
for(int i = 0; i < m_current_thread_no; ++i){
ThreadStates curState = m_states[i].load();
//cout << static_cast<int>(curState) << endl;
if(curState == ThreadStates::waiting){
tmp = false;
break;
}
}
if(tmp){
m_threads[m_current_thread_no] = thread(Worker(this, m_current_thread_no));
m_states[m_current_thread_no].store(ThreadStates::waiting);
m_current_thread_no++;
}
}
#endif