-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadManager.h
More file actions
74 lines (58 loc) · 1.24 KB
/
ThreadManager.h
File metadata and controls
74 lines (58 loc) · 1.24 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
#include <array>
#include <vector>
#include <queue>
#include <future>
#include "Def.h"
NSP_STD
NSP_CHR
#pragma once
class ThreadManager //needs taskEvaluator, distributed Processing
{
public:
class CallableObject {
public:
virtual int run() = 0;
};
private:
typedef vector<CallableObject*> UpdateRegistry;
class Threads;
class TaskDistributor { //needs lock
friend class Threads;
public:
TaskDistributor();
void registerCallable(CallableObject*);
void addTask(function<int()>);
void notify();
size_t getTasks() { return _tasks.size(); }
~TaskDistributor();
private:
function<int()> getTask();
mutex _lock;
bool _notified;
condition_variable _notifier;
UpdateRegistry _registry;
queue<function<int()>> _tasks;
};
class Threads {
public:
Threads();
explicit Threads(ThreadManager::TaskDistributor* distributor);
void start();
~Threads();
private:
void run();
const static unsigned _concurrency;
bool _running;
std::vector<thread> _threads;
TaskDistributor* _distributor;
time_point<system_clock> lastTick;
};
public:
ThreadManager();
void registerCallable(CallableObject* obj);
void addTask(function<int()>);
~ThreadManager();
private:
Threads _threadPool;
TaskDistributor _distributor;
};