-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPSCQueue.cpp
More file actions
74 lines (63 loc) · 2.08 KB
/
Copy pathSPSCQueue.cpp
File metadata and controls
74 lines (63 loc) · 2.08 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
#include <atomic>
#include <iostream>
#include <cstddef>
#include <array>
template<typename T, size_t Size>
class SPSCQueue {
public:
static_assert(Size > 0, "Size must be greater than 0");
SPSCQueue() = default;
~SPSCQueue() = default;
SPSCQueue(const SPSCQueue& other) = delete;
SPSCQueue& operator=(const SPSCQueue& other) = delete;
bool push(const T& item) {
const size_t current_tail = _tail.load(std::memory_order_relaxed);
const size_t current_head = _head.load(std::memory_order_acquire);
if (current_tail - current_head == _capacity) {
return false;
}
_buffer[current_tail % _capacity] = item;
_tail.store(current_tail + 1, std::memory_order_release);
return true;
}
bool pop(T* item) {
if (item == nullptr) {
return false;
}
const size_t current_head = _head.load(std::memory_order_relaxed);
const size_t current_tail = _tail.load(std::memory_order_acquire);
if (current_head == current_tail) {
return false;
}
*item = _buffer[current_head % _capacity];
_head.store(current_head + 1, std::memory_order_release);
return true;
}
bool empty() const {
return _head.load(std::memory_order_acquire) == _tail.load(std::memory_order_acquire);
}
bool full() const {
const size_t head = _head.load(std::memory_order_acquire);
const size_t tail = _tail.load(std::memory_order_acquire);
return tail - head == _capacity;
}
size_t size() const {
const size_t head = _head.load(std::memory_order_acquire);
const size_t tail = _tail.load(std::memory_order_acquire);
return tail - head;
}
private:
alignas(64) std::atomic<size_t> _tail{0};
alignas(64) std::atomic<size_t> _head{0};
std::array<T, Size> _buffer;
static constexpr size_t _capacity = Size;
};
int main () {
SPSCQueue<int, 10> queue;
queue.push(1);
queue.push(2);
int a;
queue.pop(&a);
std::cout << "a=:" << a << std::endl;
return 0;
}