-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrentStack.h
More file actions
114 lines (102 loc) · 3.33 KB
/
Copy pathConcurrentStack.h
File metadata and controls
114 lines (102 loc) · 3.33 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
#pragma once
#include <stack>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <sstream>
#include <iostream>
#include <cstring>
#include <chrono>
/*
* Thread safe concurrent Stack with a fixed capacity.
* It's a LIFO: items are popped out in the inverse order they've been pushed in.
* The push operations are blocked when the queue is full.
*/
template <typename T>
class ConcurrentStack
{
public:
/**
* Constructor
* @param capacity if negative, the stack is only limited by the available memory
*/
ConcurrentStack(const size_t capacity = 10) :
max_size_(capacity), bounded_(capacity > 0) {}
ConcurrentStack(const size_t capacity, const std::chrono::milliseconds ms) :
max_size_(capacity), bounded_(capacity > 0), timeout_(ms) {}
ConcurrentStack(const ConcurrentStack& rhs){
std::lock_guard<std::mutex> lock(mutex_);
stack_ = rhs.stack_;
bounded_ = rhs.bounded_;
max_size_ = rhs.max_size_;
}
ConcurrentStack<T>& operator=(const ConcurrentStack<T>& rhs) {
std::lock_guard<std::mutex> mlock(mutex_);
if (this == &rhs) return *this;
while(!stack_.empty()) { pop(); }
bounded_ = rhs.bounded_;
max_size_ = rhs.max_size_;
stack_ = rhs.stack_;
return *this;
}
T top() {
std::unique_lock<std::mutex> mlock(mutex_);
while (stack_.empty()){
std::cout << "Can't read : queue is empty !" << std::endl;
if(timeout_ == std::chrono::milliseconds(0))
queue_empty_.wait(mlock);
else
queue_empty_.wait_for(mlock, timeout_);
}
auto item = stack_.top();
mlock.unlock();
queue_full_.notify_one();
return item;
}
T pop(){
std::unique_lock<std::mutex> mlock(mutex_);
while (stack_.empty()) {
std::cout << "Can't pop : queue is empty !" << std::endl;
if(timeout_ == std::chrono::milliseconds(0))
queue_empty_.wait(mlock);
else
queue_empty_.wait_for(mlock, timeout_);
}
auto item = stack_.top();
stack_.pop();
mlock.unlock();
queue_full_.notify_one();
return item;
}
void push(const T&& item) {
{
std::unique_lock<std::mutex> mlock(mutex_);
while (bounded_ && stack_.size() >= max_size_) {
std::cout << "Can't push : queue is full !\n" << std::endl;
queue_full_.wait_for(mlock, timeout_);
}
stack_.emplace(std::move(item));
}
queue_empty_.notify_one();
}
inline size_t size() noexcept{
std::lock_guard<std::mutex> mlock(mutex_);
return stack_.size();
}
inline bool is_empty() noexcept {
std::lock_guard<std::mutex> mlock(mutex_);
return stack_.empty();
}
inline bool is_full() noexcept {
std::lock_guard<std::mutex> mlock(mutex_);
return bounded_ && stack_.size() >= max_size_;
}
private:
std::stack<T> stack_;
size_t max_size_;
bool bounded_;
std::chrono::milliseconds timeout_{0};
std::mutex mutex_{};
std::condition_variable queue_full_{}; // blocks when the stack is full
std::condition_variable queue_empty_{}; // blocks when the stack is empty
};