-
Notifications
You must be signed in to change notification settings - Fork 14
/
safe_stack.hpp
61 lines (44 loc) · 1.28 KB
/
safe_stack.hpp
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
#ifndef _SAFE_STACK_HPP_
#define _SAFE_STACK_HPP_
#include <deque>
#include <mutex>
#include <condition_variable>
template <typename T>
class SafeStack {
private:
using size_type = typename std::deque<T>::size_type;
std::deque<T> stack;
size_type max_size;
std::mutex mutex;
std::condition_variable cond_var_push;
std::condition_variable cond_var_pop;
public:
explicit SafeStack(size_type size): max_size(size) { };
SafeStack(const SafeStack&) = delete;
SafeStack& operator= (const SafeStack&) = delete;
template<typename ...Args>
void emplace(Args&&... args) {
T instance(std::forward<Args>(args)...);
std::unique_lock<std::mutex> lock(mutex);
cond_var_push.wait(lock, [=]{ return !full(); });
stack.emplace_back(std::move(instance));
lock.unlock();
cond_var_pop.notify_one();
}
T pop() {
std::unique_lock<std::mutex> lock(mutex);
cond_var_pop.wait(lock, [=]{ return !empty(); });
T ret(std::move(stack.back()));
stack.pop_back();
lock.unlock();
cond_var_push.notify_one();
return ret;
}
bool empty() const {
return stack.size() == 0;
}
bool full() const {
return stack.size() >= max_size;
}
};
#endif