-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_queue.cpp
More file actions
52 lines (38 loc) · 1.25 KB
/
Copy pathblock_queue.cpp
File metadata and controls
52 lines (38 loc) · 1.25 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
#pragma once
#include "tagged_semaphore.hpp"
#include <deque>
namespace solutions {
// Bounded Blocking Multi-Producer/Multi-Consumer (MPMC) Queue
template <typename T>
class BlockingQueue {
public:
explicit BlockingQueue(size_t capacity)
: semaphore_put_(capacity), semaphore_pop_(0), semaphore_for_move_(1) {
}
// Inserts the specified element into this queue,
// waiting if necessary for space to become available.
void Put(T value) {
auto token = semaphore_put_.Acquire();
auto ticket = semaphore_for_move_.Acquire();
deque_.push_back(std::move(value));
semaphore_for_move_.Release(std::move(ticket));
semaphore_pop_.Release(std::move(token));
}
// Retrieves and removes the head of this queue,
// waiting if necessary until an element becomes available
T Take() {
auto token = semaphore_pop_.Acquire();
auto ticket = semaphore_for_move_.Acquire();
T item = std::move(deque_.front());
deque_.pop_front();
semaphore_for_move_.Release(std::move(ticket));
semaphore_put_.Release(std::move(token));
return item;
}
private:
std::deque<T> deque_;
TaggedSemaphore<T> semaphore_put_;
TaggedSemaphore<T> semaphore_pop_;
TaggedSemaphore<T> semaphore_for_move_;
};
} // namespace solutions