非常快速的无锁队列(线程安全)
Inspired by a talk at CPPCon 2022: Trading at Light Speed, I wanted to explore lock-free queue designs that avoid the overhead of mutexes while maintaining high performance and concurrency.
A lock-free queue eliminates costly locks and context switches by relying on atomic operations. These queues enable fast message passing between threads with minimal contention.
Using locks and mutexes introduces significant performance issues:
Instead of locks, atomic operations and versioning techniques allow for efficient synchronization while ensuring correctness.
A Single Producer Multiple Consumer (SPMC) queue allows one writer to produce messages and multiple consumers to read them.
As David Gross explains in Trading at Light Speed, the first version of an SPMC queue uses two global atomic indices to synchronize reads and writes.
While effective, this approach has high contention because all consumers and the producer update the same global indices.
A better approach is to assign each queue element its own version number. This reduces contention because:
This technique minimizes atomic contention, making it significantly faster than global indices.
A Single Producer Single Consumer (SPSC) queue ensures that each data block is read exactly once before being overwritten.
std::atomic unread to track whether the data has been read.A potential issue arises if a writer starts overwriting a block before the reader has finished reading. The solution:
unread to false before writing to prevent concurrent reads.compare_exchange_strong ensures only one reader can consume the block.mVersion:mVersion is odd, a reader is reading → do not overwrite.mVersion is even, it's safe to write.This technique ensures safe, efficient, and low-latency communication between a producer and a single consumer.
Each element in the queue maintains its own version number, reducing contention and improving cache locality.
struct Block
{
// Local block versions reduce contention for the queue
std::atomic mVersion;
// Size of the data
std::atomic mSize;
// 64-byte buffer
alignas(64) uint8_t mData[64];
};0 (no reads allowed).First write:
1 (odd → read allowed).Rewriting:
2 (ready for reuse).This method ensures safe message passing and minimizes unnecessary atomic updates.
Note: This design can be slightly modified to make the queue load balance messages instead of multicasting.
The SPMC Queue in spmc_q.h was tested for the number of messages processed by the consumers over 5 seconds with a blocking queue using mutexes and locks, and the boost::lockfree::queue.
This is not totally a fair comparison as the blocking queue and the boost::lockfree::queue are not multicast.
From the histogram above, you can see the SPMC Queue completely overtakes the other 2 queues. Note: The numbers should be looked at relatively compared to the other queues as the specific performance is system and compiler dependent.
The code for SPMC Queue and all the benchmarking done is available in the src folder.
This project is licensed under the MIT License – see the LICENSE file for details.
暂无开放 Issues,或尚未同步最近议题。