-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadManager.cpp
142 lines (117 loc) · 2.72 KB
/
ThreadManager.cpp
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
#include "ThreadManager.h"
#include <chrono>
#include <thread>
#include "Def.h"
#include "Log.h"
NSP_STD
NSP_CHR
const unsigned ThreadManager::Threads::_concurrency = thread::hardware_concurrency();
ThreadManager::ThreadManager() :
_distributor(),
_threadPool(&_distributor)
{
_threadPool.start();
}
void ThreadManager::registerCallable(CallableObject * obj)
{
_distributor.registerCallable(obj);
}
void ThreadManager::addTask(function<int()>func)
{
_distributor.addTask(func);
}
ThreadManager::~ThreadManager()
{}
ThreadManager::Threads::Threads()
{
_running = false;
_threads.clear();
_distributor = nullptr;
}
ThreadManager::Threads::Threads(ThreadManager::TaskDistributor* distributor) :
_distributor(distributor),
_running(false)
{
_threads.clear();
}
void ThreadManager::Threads::start()
{
_running = true;
for (unsigned char i = 0; i < _concurrency; i++)
_threads.push_back(move(thread()));
for (unsigned char i = 0; i < _concurrency; i++)
_threads[i].swap(thread([this] {run(); }));
}
void ThreadManager::Threads::run()
{
int err = 0;
while (err == 0 && _running)
{
lastTick = chrono::system_clock::now();
function<int()> task = _distributor->getTask();
_distributor->notify();
task();
int duration = 20 - _distributor->getTasks();
if (duration > 0) //prevents conversion from signed to unsigned
this_thread::sleep_for(milliseconds(duration) - duration_cast<chrono::milliseconds>(system_clock::now() - lastTick));
}
}
ThreadManager::Threads::~Threads()
{
_running = false;
for (unsigned char i = 0; i < _concurrency; i++)
_threads[i].join();
}
ThreadManager::TaskDistributor::TaskDistributor() : _registry(), _tasks(), _notifier(), _notified(false)
{
}
void ThreadManager::TaskDistributor::registerCallable(CallableObject * callable)
{
std::unique_lock<std::mutex> lk(_lock);
_registry.push_back(callable);
notify();
}
void ThreadManager::TaskDistributor::addTask(function<int()> task)
{
std::unique_lock<std::mutex> lk(_lock);
_tasks.push(task);
notify();
}
void ThreadManager::TaskDistributor::notify()
{
_notified = true;
_notifier.notify_one();
}
ThreadManager::TaskDistributor::~TaskDistributor()
{
std::lock_guard<std::mutex> lk(_lock);
while (!_tasks.empty())
_tasks.pop();
_registry.clear();
}
function<int()> ThreadManager::TaskDistributor::getTask()
{
std::unique_lock<std::mutex> lk(_lock);
while (!_notified)
{
_notifier.wait(lk);
}
if (_tasks.empty())
{
for (CallableObject* obj : _registry)
if (obj)
_tasks.push([obj] { return obj->run(); });
else
LOG << "Invalid Task Object passed.";
}
if (!_tasks.empty())
{
function<int()> task = _tasks.front();
_tasks.pop();
_notified = false;
return task;
}
_notified = false;
return
[] {return 0; };
}