31 ThreadSafeDeque(
size_t notify_size = 100) : _notify_size(notify_size) {}
34 this->data = other.data;
35 this->_notify_size = other._notify_size;
36 this->_is_empty.store(other._is_empty.load(std::memory_order_acquire), std::memory_order_release);
41 this->_is_empty.store(source.empty(), std::memory_order_release);
44 bool empty()
const noexcept {
45 return this->_is_empty.load(std::memory_order_acquire);
49 std::lock_guard<std::mutex> lock{ this->_lock };
50 return this->data.front();
62 void push_back(T&& item) {
63 std::lock_guard<std::mutex> lock{ this->_lock };
64 this->data.push_back(std::move(item));
65 this->_is_empty.store(
false, std::memory_order_release);
67 if (this->data.size() >= _notify_size) {
68 this->_cond.notify_all();
72 T pop_front() noexcept {
73 std::lock_guard<std::mutex> lock{ this->_lock };
74 T item = std::move(data.front());
77 if (this->data.empty()) {
78 this->_is_empty.store(
true, std::memory_order_release);
86 return this->_is_waitable.load(std::memory_order_acquire);
94 std::unique_lock<std::mutex> lock{ this->_lock };
95 this->_cond.wait(lock, [
this] {
return this->data.size() >= _notify_size || !this->
is_waitable(); });
99 size_t size() const noexcept {
100 std::lock_guard<std::mutex> lock{ this->_lock };
101 return this->data.size();
104 typename std::deque<T>::iterator begin() noexcept {
105 return this->data.begin();
108 typename std::deque<T>::iterator end() noexcept {
109 return this->data.end();
114 std::lock_guard<std::mutex> lock{ this->_lock };
115 this->_is_waitable.store(
true, std::memory_order_release);
116 this->_cond.notify_all();
120 std::lock_guard<std::mutex> lock{ this->_lock };
121 this->_is_waitable.store(
false, std::memory_order_release);
122 this->_cond.notify_all();
126 std::atomic<bool> _is_empty{
true };
127 std::atomic<bool> _is_waitable{
false };
129 mutable std::mutex _lock;
130 std::condition_variable _cond;