-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockfreequeue.h
87 lines (70 loc) · 2.47 KB
/
lockfreequeue.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
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
/*
* A thread-safe FIFO queue for a one creator - one consumer scenario
* Avoids use of lock/mutex structures to speed up access.
* Pushes non-blocking, pops blocking.
* When input is finished, ::done() must be called, else the last
* pop will wait infinitely for more elements to appear.
*/
#ifndef LOCKFREEQUEUE
#define LOCKFREEQUEUE
#include <atomic>
template <class T>
class LockFreeQueue {
private:
struct Node {
Node ( T val ) : value(val), next(nullptr) {}
T value;
Node* next;
};
Node* head; // marks first element
std::atomic<Node*> divider; // marks first not-consumed element
std::atomic<Node*> tail; // marks last element for appending nodes
std::atomic<bool> done_; // has appending finished?
public:
LockFreeQueue ();
~LockFreeQueue ();
void push ( const T& data ); // push one data element to queue, nonblocking
bool pop ( T& dest ); // delete first element from queue and store it in dest
bool isDone(); // check if all elenemts have been pushed
void done ( bool flag = true ); // set or reset done flag
};
template <class T>
LockFreeQueue<T>::LockFreeQueue() {
head = divider = tail = new Node( T() ); // create dummy
done_ = false;
}
template <class T>
LockFreeQueue<T>::~LockFreeQueue () { // free remaining pointers
while (head != nullptr) {
Node* tmp = head->next;
delete head;
head = tmp;
}
}
template <class T>
void LockFreeQueue<T>::push ( const T& data ) {
Node* pTail = tail.load(); // get atomic state
pTail->next = new Node(data); // construct new node
tail = pTail->next; // update atomic state
while (head != divider) { // clean up data that has already been consumed...
Node* tmp = head;
head = head->next;
delete tmp;
}
}
template <class T>
bool LockFreeQueue<T>::pop ( T& dest ) {
while (!done_ && divider == tail); // no elements, but elements expected: wait for element
if (divider != tail) { // if unconsumed elements exist...
Node* div = divider.load(); // load atomic state
dest = div->next->value;
divider = div->next; // advance atomic state
return true; // success
}
return false; // done was set and no more elements found - return false to end consumer loop
}
template <class T>
bool LockFreeQueue<T>::isDone() { return done_; }
template <class T>
void LockFreeQueue<T>::done ( bool flag ) { done_ = flag; }
#endif // LOCKFREEQUEUE