-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPool.h
59 lines (56 loc) · 1001 Bytes
/
Pool.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
#ifndef __POOL_H__
#define __POOL_H__
#include "Mutex.h"
#include <queue>
#include <unistd.h>
template <class T> class Pool
{
private:
std::queue<T> data;
Mutex mtx;
size_t max_size;
size_t spin_time;
public:
Pool(size_t max_size = 10, size_t spin_time = 50)
: max_size(max_size), spin_time(spin_time)
{}
~Pool()
{
mtx.lock();
while (data.size()) data.pop();
mtx.unlock();
}
size_t size()
{
mtx.lock();
size_t result = data.size();
mtx.unlock();
return result;
}
void push(T item)
{
mtx.lock();
while (data.size() >= max_size)
{
mtx.unlock();
usleep(spin_time);
mtx.lock();
}
data.push(item);
mtx.unlock();
}
T pop()
{
mtx.lock();
while (data.size() <= 0)
{
mtx.unlock();
usleep(spin_time);
mtx.lock();
}
T item = data.front(); data.pop();
mtx.unlock();
return item;
}
};
#endif