42 explicit Array(int64_t c) :
46 S {
new T[
static_cast<size_t>(C)]} {
64 void push(int64_t i, O&& o) noexcept {
67 S[i & M] = std::forward<O>(o);
70 T
pop(int64_t i) noexcept {
75 Array* resize(int64_t b, int64_t t) {
76 Array* ptr =
new Array {2*C};
77 for(int64_t i=t; i!=b; ++i) {
108 bool empty()
const noexcept;
113 size_t size()
const noexcept;
131 template <
typename O>
140 std::optional<T>
pop();
148 std::optional<T>
steal();
152 template <
typename T>
154 assert(c && (!(c & (c-1))));
155 _top.store(0, std::memory_order_relaxed);
156 _bottom.store(0, std::memory_order_relaxed);
157 _array.store(
new Array{c}, std::memory_order_relaxed);
158 _garbage.reserve(32);
162 template <
typename T>
164 for(
auto a : _garbage) {
167 delete _array.load();
171 template <
typename T>
173 int64_t b = _bottom.load(std::memory_order_relaxed);
174 int64_t t = _top.load(std::memory_order_relaxed);
179 template <
typename T>
181 int64_t b = _bottom.load(std::memory_order_relaxed);
182 int64_t t = _top.load(std::memory_order_relaxed);
183 return static_cast<size_t>(b >= t ? b - t : 0);
187 template <
typename T>
188 template <
typename O>
190 int64_t b = _bottom.load(std::memory_order_relaxed);
191 int64_t t = _top.load(std::memory_order_acquire);
192 Array* a = _array.load(std::memory_order_relaxed);
195 if(a->capacity() - 1 < (b - t)) {
196 Array* tmp = a->resize(b, t);
197 _garbage.push_back(a);
199 _array.store(a, std::memory_order_relaxed);
202 a->push(b, std::forward<O>(o));
203 std::atomic_thread_fence(std::memory_order_release);
204 _bottom.store(b + 1, std::memory_order_relaxed);
208 template <
typename T>
210 int64_t b = _bottom.load(std::memory_order_relaxed) - 1;
211 Array* a = _array.load(std::memory_order_relaxed);
212 _bottom.store(b, std::memory_order_relaxed);
213 std::atomic_thread_fence(std::memory_order_seq_cst);
214 int64_t t = _top.load(std::memory_order_relaxed);
216 std::optional<T> item;
222 if(!_top.compare_exchange_strong(t, t+1,
223 std::memory_order_seq_cst,
224 std::memory_order_relaxed)) {
227 _bottom.store(b + 1, std::memory_order_relaxed);
231 _bottom.store(b + 1, std::memory_order_relaxed);
238 template <
typename T>
240 int64_t t = _top.load(std::memory_order_acquire);
241 std::atomic_thread_fence(std::memory_order_seq_cst);
242 int64_t b = _bottom.load(std::memory_order_acquire);
244 std::optional<T> item;
247 Array* a = _array.load(std::memory_order_consume);
249 if(!_top.compare_exchange_strong(t, t+1,
250 std::memory_order_seq_cst,
251 std::memory_order_relaxed)) {
260 template <
typename T>
262 return _array.load(std::memory_order_relaxed)->capacity();
bool empty() const noexcept
queries if the queue is empty at the time of this call
Definition: spmc_queue.hpp:172
~WorkStealingQueue()
destructs the queue
Definition: spmc_queue.hpp:163
void push(O &&item)
inserts an item to the queue
Definition: spmc_queue.hpp:189
Definition: taskflow.hpp:5
size_t size() const noexcept
queries the number of items at the time of this call
Definition: spmc_queue.hpp:180
int64_t capacity() const noexcept
queries the capacity of the queue
Definition: spmc_queue.hpp:261
std::optional< T > pop()
pops out an item from the queue
Definition: spmc_queue.hpp:209
Lock-free unbounded single-producer multiple-consumer queue.
Definition: spmc_queue.hpp:29
std::optional< T > steal()
steals an item from the queue
Definition: spmc_queue.hpp:239
WorkStealingQueue(int64_t capacity=1024)
constructs the queue with a given capacity
Definition: spmc_queue.hpp:153