r/programming • u/Dear-Economics-315 • Jul 27 '26
Building a Fast Lock-Free Queue in Modern C++ From Scratch
https://blog.jaysmito.dev/blog/04-fast-lockfree-queues/
68
Upvotes
r/programming • u/Dear-Economics-315 • Jul 27 '26
19
u/trailing_zero_count Jul 27 '26
If you're going to use hazard pointers with an unbounded queue, then you can also use fetch_add instead of compare_exchange to get a write or read ticket. This gives much better performance under contention. I have such an implementation here: https://github.com/tzcnt/coro_util/blob/main/include/coro_util/channel.hpp
My hazard pointer behavior is based on the paper 'A wait-free queue as fast as fetch-and-add' by Yang & Mellor-Crummey. The author has a C implementation here: https://github.com/chaoran/fast-wait-free-queue/blob/master/wfqueue.c
9
u/ReDucTor Jul 28 '26 edited Jul 28 '26
Some feedback
PointerCountalso seems a strange choice for any of those use cases you could just create a newHazardPointerinstance and you would use the same amount of space and reduce the amount of pointers to be scanned.tcBuffand it's assocation withThreadCountseems to indicate that this is the case.FastQueueNodebeing bounded forPushyou can do afetch_addand check if it's aboveSizeand throw away if it is otherwise store the data and set the flag.FORCE_INLINElot of these functions are not simple and it will compile time and potentially even runtime with heavily abusingFORCE_INLINEatomic_flagwithtest_and_setwhen your just discarding the result on x86 is typically going to result in anxchg(on clang, gcc, and msvc) where if you just used anatomic<bool>withstoreandmemory_order_releaseit will be a normalmovFastQueueNode::Popwith blocking does not look like it will not block if the queue is empty, only if the entry has been allocated filled but not filled. The same with the higherFastQueue::Popoften it's useful for a MPMC to be the one that controls when threads go to sleep and wakeIsEmptyare dangerous to have on a queue, they are potentially invalid by the time they return, especially if you expect to use those because pop does not do proper blocking.FastQueueNodeneedseq_cst, you mention in the post about global consistency with the hazard pointer tracking however it does not use hazard pointers internally onlyFastQueuedoesHazardPointer? You want nothing to move out of theProtect/Releaserange and nothing moving beyondRetire, then for anyScanto have a full view of the other threadsProtected pointers globally so the pointer that is protected should not need the seq_cst however the stored hazard pointer will probably need it for a global ordering however you might be able to get away with release + seq_cst fencememory_order_releaseforFastQueueNodeSlot::Resetto ensure that it is resetting after it has been used.currentTail->nextbeingnullptryou essentially force there to always be atleast twoFastQueueNode's even if it was a single thread producing and consuming its own items11
u/matthieum Jul 27 '26
Holy molly, what a monster!
The API was weird to start with -- raw pointers, in MY C++ codebase? -- and it only got worse -- hazard pointers, really? -- without any justification for the complexity.
Why, oh why, are not values passed in/out instead? Let the user decide what values, and they may pass
T*orstd::unique_ptr<T>orstd::shared_ptr<T>as they see fit.2
u/[deleted] Jul 28 '26
[removed] — view removed comment