SPSCQueue单一消费者等待和无锁定大小队列
SPSCQueue 是用 C++ 11 编写的单个生产者单一消费者等待和无锁定大小队列。
示例代码
SPSCQueue<int> q(2); auto t = std::thread([&] { while (!q.front()); std::cout << *q.front() << std::endl; q.pop(); }); q.push(1); t.join();
使用
-
SPSCQueue<T>(size_t capacity);
Create a
SPSCqueue
holding items of typeT
with capacitycapacity
. Capacity need to be greater than 2. -
void emplace(Args &&... args);
Enqueue an item using inplace construction. Blocks if queue is full.
-
bool try_emplace(Args &&... args);
Try to enqueue an item using inplace construction. Returns
true
on success andfalse
if queue is full. -
void push(const T &v);
Enqueue an item using copy construction. Blocks if queue is full.
-
template <typename P> void push(P &&v);
Enqueue an item using move construction. Participates in overload resolution only if
std::is_constructible<T, P&&>::value == true
. Blocks if queue is full. -
bool try_push(const T &v);
Try to enqueue an item using copy construction. Returns
true
on success andfalse
if queue is full. -
template <typename P> void try_push(P &&v);
Try to enqueue an item using move construction. Returns
true
on success andfalse
if queue is full. Participates in overload resolution only ifstd::is_constructible<T, P&&>::value == true
. -
T *front();
Return pointer to front of queue. Returns
nullptr
if queue is empty. -
pop();
Dequeue first elment of queue. Invalid to call if queue is empty. Requires
std::is_nothrow_destructible<T>::value == true
.
一旦一个单一的写线程可以执行入队列的操作时,只有一个单一的读线程可以执行取队列操作,其他的使用都是不允许的。
实现原理
底层实现是一个环形缓冲区。
参考资料:
- Intel. Avoiding and Identifying False Sharing Among Threads.
- Wikipedia. Ring buffer.
- Wikipedia. False sharing.
性能测试
以下测试结果是基于 2 socket machine with 2 x Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz.
NUMA Node / Core / Hyper-Thread | Throughput (ops/ms) | Latency RTT (ns) |
---|---|---|
#0,#0,#0 & #0,#0,#1 | 63942 | 60 |
#0,#0,#0 & #0,#1,#0 | 37739 | 238 |
#0,#0,#0 & #1,#0,#0 |