Repository navigation
Expand file tree
/
Copy pathRingBuffer.cpp
More file actions
115 lines (92 loc) · 2.2 KB
/
Copy pathRingBuffer.cpp
File metadata and controls
115 lines (92 loc) · 2.2 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
115
#include <iostream>
#include <cassert>
#include <mutex>
/*
# To compile:
g++ -std=c++20 -o RingBuffer.o RingBuffer.cpp
# without -o, outputs to default a.out
# without -std=c++20, std::scoped_lock calls fail
# To run:
./RingBuffer.o
*/
template<typename T>
struct RingBuffer
{
RingBuffer(size_t capacity)
{
_capacity = capacity;
_data = new T[_capacity];
_writer = 0;
_reader = 0;
_size = 0;
}
~RingBuffer(){
delete[] _data;
}
size_t size()
{
//_m.lock();
return _size;
//_m.unlock();
}
void enqueue(const T& e)
{
//_m.lock();
//std::lock_guard<std::mutex> lock(_m);
std::scoped_lock lock(_m);
if (size() >= _capacity) {
// If using plain mutex.lock(), it doesn't get unlocked when error is thrown
throw std::runtime_error("Buffer is full");
}
_data[_writer % _capacity] = e;
_writer = _writer + 1;
_size = _size + 1;
//_m.unlock();
}
T dequeue()
{
//_m.lock();
std::scoped_lock lock(_m);
//std::unique_lock<std::mutex> lock(_m);
//std::lock_guard<std::mutex> lock(_m);
//TODO: replace with context aware mutex
if (size() == 0) { throw std::runtime_error("Buffer is empty"); }
auto popped = _reader % _capacity;
_reader = _reader + 1;
_size = _size - 1;
//_m.unlock();
return _data[popped];
}
private:
std::mutex _m;
T* _data;
uint16_t _capacity, _reader, _writer, _size;
};
int main(int argc, const char* argv[])
{
RingBuffer<int> rb(1);
/* Test cases */
rb.enqueue(3);
assert(rb.size() == 1);
bool error = false;
try{
rb.enqueue(4);
}catch(std::runtime_error e){
error = true;
}/*catch(...){
error = true;
}*/
assert(error);
error = false;
auto popped = rb.dequeue();
assert(popped == 3);
assert(rb.size() == 0);
try{
popped = rb.dequeue();
}catch(std::runtime_error e){
error = true;
}
assert(error);
std::cout << "Success!" << std::endl;
return 0;
}