forked from cameron314/concurrentqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlockbasedqueue.h
96 lines (83 loc) · 2.02 KB
/
lockbasedqueue.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
88
89
90
91
92
93
94
95
96
// ©2013-2014 Cameron Desrochers.
// Distributed under the simplified BSD license (see the LICENSE file that
// should have come with this file).
#pragma once
#include "wrappers.h"
#include <mutex>
// Naïve implementation of a simple lock-based queue. A std::mutex is obtained for every
// method. Note that while the queue size is not fixed, each enqueue operation allocates
// memory, and each dequeue frees memory.
template<typename T>
class LockBasedQueue
{
public:
typedef DummyToken producer_token_t;
typedef DummyToken consumer_token_t;
public:
LockBasedQueue()
{
tail = nullptr;
head = nullptr;
}
~LockBasedQueue()
{
while (head != nullptr) {
Node* next = head->next;
delete head;
head = next;
}
}
template<typename U>
inline bool enqueue(U&& item)
{
Node* node = new Node(item);
std::lock_guard<std::mutex> guard(mutex);
if (tail == nullptr) {
head = tail = node;
}
else {
tail->next = node;
tail = node;
}
return true;
}
inline bool try_dequeue(T& item)
{
std::lock_guard<std::mutex> guard(mutex);
if (head == nullptr) {
return false;
}
else {
item = std::move(head->item);
Node* next = head->next;
delete head;
head = next;
if (head == nullptr) {
tail = nullptr;
}
return true;
}
}
// Dummy token methods (not used)
bool enqueue(producer_token_t const&, T const&) { return false; }
bool try_enqueue(producer_token_t, T const&) { return false; }
bool try_dequeue(consumer_token_t, T& item) { return false; }
template<typename It> bool enqueue_bulk(It, size_t) { return false; }
template<typename It> bool enqueue_bulk(producer_token_t const&, It, size_t) { return false; }
template<typename It> size_t try_dequeue_bulk(It, size_t) { return 0; }
template<typename It> size_t try_dequeue_bulk(consumer_token_t, It, size_t) { return 0; }
private:
struct Node
{
T item;
Node* next;
template<typename U>
Node(U&& item)
: item(std::forward<U>(item)), next(nullptr)
{
}
};
std::mutex mutex;
Node* head;
Node* tail;
};