-
Notifications
You must be signed in to change notification settings - Fork 1
/
ObjectPool.h
66 lines (55 loc) · 1.45 KB
/
ObjectPool.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
#include <memory>
#include <chrono>
#include <thread>
#include <tbb/concurrent_queue.h>
#include <tbb/tbb_thread.h>
template <class T>
class ObjectPool
{
private:
struct External_Deleter {
explicit External_Deleter(std::weak_ptr<ObjectPool<T>* > pool)
: pool_(pool) {}
void operator()(T* ptr) {
if (auto pool_ptr = pool_.lock()) {
try {
(*pool_ptr.get())->add(std::unique_ptr<T>{ptr});
return;
} catch(...) {}
}
std::default_delete<T>{}(ptr);
}
private:
std::weak_ptr<ObjectPool<T>* > pool_;
};
public:
using ptr_type = std::unique_ptr<T, External_Deleter >;
ObjectPool() : this_ptr_(new ObjectPool<T>*(this)) {}
virtual ~ObjectPool(){}
void add(std::unique_ptr<T>&& t) {
pool_.push(std::move(t));
}
std::unique_ptr<T> remove() {
std::unique_ptr<T> ptr;
while (!pool_.try_pop(ptr)) std::this_thread::sleep_for(std::chrono::milliseconds(1));
return ptr;
}
ptr_type acquire() {
std::unique_ptr<T> ptr;
while (!pool_.try_pop(ptr)) std::this_thread::sleep_for(std::chrono::milliseconds(1));
return ptr_type(ptr.release(), External_Deleter{ std::weak_ptr<ObjectPool<T>*>{this_ptr_} });
}
void clear()
{
pool_.clear();
}
bool empty() const {
return pool_.empty();
}
size_t size() const {
return pool_.unsafe_size();
}
private:
std::shared_ptr<ObjectPool<T>* > this_ptr_;
tbb::concurrent_queue<std::unique_ptr<T> > pool_;
};