forked from marcdinkum/ringbuffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathringbuffer.h
More file actions
44 lines (29 loc) · 938 Bytes
/
ringbuffer.h
File metadata and controls
44 lines (29 loc) · 938 Bytes
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
//ringbuffer.h
#include <atomic>
#include <string>
template<typename FloatType>
class RingBuffer
{
public:
using uint64 = unsigned long;
RingBuffer(uint64 size, const std::string& name);
~RingBuffer();
auto push(FloatType* data, uint64 numSamples) -> uint64;
auto pop(FloatType* data, uint64 numSamples) -> uint64;
auto numItemsAvailableForWrite() const -> uint64;
auto numItemsAvailableForRead() const -> uint64;
auto isLockFree() const -> bool;
auto pushMayBlock(bool block) -> void;
auto popMayBlock(bool block) -> void;
auto setBlockingNap(uint64 blockingNap) -> void;
private:
uint64 size;
FloatType* buffer;
std::atomic<uint64> tail; // write index
std::atomic<uint64> head; // read index
static constexpr uint64 itemSize { sizeof(FloatType) };
const std::string name;
bool blockingPush;
bool blockingPop;
uint64 blockingNap { 500 };
};