| 1 |
// https://github.com/cameron314/concurrentqueue/raw/master/benchmarks/lockbasedqueue.h |
| 2 |
// |
| 3 |
// ©2013-2014 Cameron Desrochers. |
| 4 |
// Distributed under the simplified BSD license (see the LICENSE file that |
| 5 |
// should have come with this file). |
| 6 |
|
| 7 |
#pragma once |
| 8 |
|
| 9 |
#include <mutex> |
| 10 |
|
| 11 |
struct DummyToken |
| 12 |
{ |
| 13 |
template<typename TQueue> |
| 14 |
DummyToken(TQueue const&) |
| 15 |
{ |
| 16 |
} |
| 17 |
}; |
| 18 |
|
| 19 |
// Naïve implementation of a simple lock-based queue. A std::mutex is obtained for every |
| 20 |
// method. Note that while the queue size is not fixed, each enqueue operation allocates |
| 21 |
// memory, and each dequeue frees memory. |
| 22 |
template<typename T> |
| 23 |
class LockBasedQueue |
| 24 |
{ |
| 25 |
public: |
| 26 |
typedef DummyToken producer_token_t; |
| 27 |
typedef DummyToken consumer_token_t; |
| 28 |
|
| 29 |
public: |
| 30 |
LockBasedQueue() |
| 31 |
{ |
| 32 |
tail = nullptr; |
| 33 |
head = nullptr; |
| 34 |
nb_elements = 0; |
| 35 |
} |
| 36 |
|
| 37 |
~LockBasedQueue() |
| 38 |
{ |
| 39 |
while (head != nullptr) { |
| 40 |
Node* next = head->next; |
| 41 |
delete head; |
| 42 |
head = next; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
template<typename U> |
| 47 |
inline bool enqueue(U&& item) |
| 48 |
{ |
| 49 |
Node* node = new Node(item); |
| 50 |
|
| 51 |
std::lock_guard<std::mutex> guard(mutex); |
| 52 |
if (tail == nullptr) { |
| 53 |
head = tail = node; |
| 54 |
} |
| 55 |
else { |
| 56 |
tail->next = node; |
| 57 |
tail = node; |
| 58 |
} |
| 59 |
|
| 60 |
nb_elements++; |
| 61 |
|
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
inline bool try_dequeue(T& item) |
| 66 |
{ |
| 67 |
std::lock_guard<std::mutex> guard(mutex); |
| 68 |
if (head == nullptr) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
else { |
| 72 |
item = std::move(head->item); |
| 73 |
Node* next = head->next; |
| 74 |
delete head; |
| 75 |
head = next; |
| 76 |
if (head == nullptr) { |
| 77 |
tail = nullptr; |
| 78 |
} |
| 79 |
nb_elements--; |
| 80 |
return true; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
unsigned long size_approx() |
| 85 |
{ |
| 86 |
return nb_elements; |
| 87 |
} |
| 88 |
|
| 89 |
unsigned long overhead_per_element() |
| 90 |
{ |
| 91 |
return sizeof(Node) ; |
| 92 |
} |
| 93 |
|
| 94 |
// Dummy token methods (not used) |
| 95 |
bool enqueue(producer_token_t const&, T const&) { return false; } |
| 96 |
bool try_enqueue(producer_token_t, T const&) { return false; } |
| 97 |
bool try_dequeue(consumer_token_t, T& item) { return false; } |
| 98 |
template<typename It> bool enqueue_bulk(It, size_t) { return false; } |
| 99 |
template<typename It> bool enqueue_bulk(producer_token_t const&, It, size_t) { return false; } |
| 100 |
template<typename It> size_t try_dequeue_bulk(It, size_t) { return 0; } |
| 101 |
template<typename It> size_t try_dequeue_bulk(consumer_token_t, It, size_t) { return 0; } |
| 102 |
|
| 103 |
private: |
| 104 |
struct Node |
| 105 |
{ |
| 106 |
T item; |
| 107 |
Node* next; |
| 108 |
|
| 109 |
template<typename U> |
| 110 |
Node(U&& item) |
| 111 |
: item(std::forward<U>(item)), next(nullptr) |
| 112 |
{ |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
mutable std::mutex mutex; |
| 117 |
Node* head; |
| 118 |
Node* tail; |
| 119 |
int nb_elements; |
| 120 |
}; |