Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPool.h
More file actions
Latest commit
132 lines (104 loc) · 2.78 KB
/
Copy pathPool.h
File metadata and controls
132 lines (104 loc) · 2.78 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
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
#ifndef POOL_H
#definePOOL_H
#include<queue>
#include<mutex>
#include<stddef.h>
#include<cstdint>
#include<Utilities.h>
#include<chrono>
#include<atomic>
namespaceToolFramework{
template<classT> structPool_args : Thread_args{
Pool_args(){;}
~Pool_args(){;}
size_t object_cap;
uint16_t manage_period_ms;
std::queue<T*>* objects;
std::mutex* mtx;
std::chrono::high_resolution_clock::time_point now;
std::chrono::high_resolution_clock::time_point managing_timer;
uint64_t count;
unsignedint* sum;
unsignedint* counter;
};
template<classT> classPool{
public:
Pool(bool in_manage=true, size_t in_object_cap=1, uint16_t period_ms=1000){
counter = 0;
sum = 0;
manage=in_manage;
args.manage_period_ms=period_ms;
args.object_cap=in_object_cap;
args.sum = ∑
args.counter = &counter;
args.mtx = &mtx;
args.objects=&objects;
if(manage) m_utils.CreateThread("pool_manager", &Thread, &args);
}
~Pool(){
if(manage) m_utils.KillThread(&args);
Clear();
}
voidSetPeriod(uint16_t period_ms){
args.manage_period_ms=period_ms;
}
voidSetObjectCap(size_t in_object_cap){
args.object_cap=in_object_cap;
}
template <typename... Args> T* GetNew(Args... in_args){
mtx.lock();
counter++;
sum+=objects.size();
if(objects.size()>0){
T* tmp=objects.front();
objects.pop();
mtx.unlock();
return tmp;
}
mtx.unlock();
returnnewT(in_args...);
}
voidAdd(T* object){
mtx.lock();
objects.push(object);
mtx.unlock();
}
voidClear(){
mtx.lock();
while(!objects.empty()){
delete objects.front();
objects.pop();
}
mtx.unlock();
}
staticvoidThread(Thread_args* arg){
Pool_args<T>* args = reinterpret_cast<Pool_args<T>*>(arg);
args->count = std::chrono::duration<double, std::milli>( std::chrono::high_resolution_clock::now() - args->managing_timer).count();
if(args->count < args->manage_period_ms){
usleep((args->manage_period_ms - args->count)*1000);
return;
}
args->mtx->lock();
if( *args->counter!=0 && (((*args->sum) / (*args->counter)) > args->object_cap)){
while(args->objects->size() > args->object_cap){
delete args->objects->front();
args->objects->pop();
}
*args->counter = 0;
*args->sum = 0;
}
args->mtx->unlock();
args->managing_timer=std::chrono::high_resolution_clock::now();
return;
}
private:
std::queue<T*> objects;
std::mutex mtx;
Utilities m_utils;
Pool_args<T> args;
bool manage;
unsignedint counter;
unsignedint sum;
};
}
#endif