-
Notifications
You must be signed in to change notification settings - Fork 0
/
LockFreeQueues.h
59 lines (43 loc) · 1.38 KB
/
LockFreeQueues.h
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
#pragma once
#include <iostream>
#include <vector>
#include <atomic>
#include "macros.h"
namespace Common {
template<typename T>
class LFQueue final {
public:
LFQueue(std::size_t num_elems) :
store_(num_elems, T()) /* pre-allocation of vector storage. */ {
}
auto getNextToWriteTo() noexcept {
return &store_[next_write_index_];
}
auto updateWriteIndex() noexcept {
next_write_index_ = (next_write_index_ + 1) % store_.size();
num_elements_++;
}
auto getNextToRead() const noexcept -> const T * {
return (size() ? &store_[next_read_index_] : nullptr);
}
auto updateReadIndex() noexcept {
next_read_index_ = (next_read_index_ + 1) % store_.size();
ASSERT(num_elements_ != 0, "Read an invalid element in:" + std::to_string(pthread_self()));
num_elements_--;
}
auto size() const noexcept {
return num_elements_.load();
}
// Deleted default, copy & move constructors and assignment-operators.
LFQueue() = delete;
LFQueue(const LFQueue &) = delete;
LFQueue(const LFQueue &&) = delete;
LFQueue &operator=(const LFQueue &) = delete;
LFQueue &operator=(const LFQueue &&) = delete;
private:
std::vector<T> store_;
std::atomic<size_t> next_write_index_ = {0};
std::atomic<size_t> next_read_index_ = {0};
std::atomic<size_t> num_elements_ = {0};
};
}