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 type T with capacity capacity. Capacity need to be greater than 2.
SPSCqueue
T
capacity
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 and false if queue is full.
true
false
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.
std::is_constructible<T, P&&>::value == true
bool try_push(const T &v);
Try to enqueue an item using copy construction. Returns true on success and false if queue is full.
template <typename P> void try_push(P &&v);
Try to enqueue an item using move construction. Returns true on success and false if queue is full. Participates in overload resolution only if std::is_constructible<T, P&&>::value == true.
T *front();
Return pointer to front of queue. Returns nullptr if queue is empty.
nullptr
pop();
Dequeue first elment of queue. Invalid to call if queue is empty. Requires std::is_nothrow_destructible<T>::value == true.
std::is_nothrow_destructible<T>::value == true
一旦一个单一的写线程可以执行入队列的操作时,只有一个单一的读线程可以执行取队列操作,其他的使用都是不允许的。
底层实现是一个环形缓冲区。
参考资料:
以下测试结果是基于 2 socket machine with 2 x Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz.