r/cpp • u/hallofcheat99 • May 27 '26
Lessons I’ve learned from benchmarking lock free queues
https://open.substack.com/pub/simonlee99ap/p/should-your-system-use-lock-free?r=16x6qr&utm_medium=iosHey all, I’ve started writing about systems related topics, and as a first article I wanted to understand the tradeoffs of adopting lock free data structures. Turns out it’s hard to find an audience that’d be interested in this kind of topic, so I figured people here might be the best fit. Let me know what you think about the article! Would love to hear your thoughts
87
Upvotes
42
u/trailing_zero_count async enthusiast | TooManyCooks author May 27 '26
Your next quest: study "lock free" vs "wait free", then implement a wait free queue and report back. CAS degrades under high load as if 8 threads are hitting it, one succeeds and the other 7 fail. This causes a retry storm and can also cause unfairness/starvation. Using a primitive like FAA (fetch_add) to reserve slots guarantees that each producer or consumer is able to get a slot in a single operation. Performance will still degrade under high load due to cache line contention, but it will be much more gracefully since nobody needs to retry.
10
u/tialaramex May 27 '26
Both "lock freedom" and "wait freedom" are best understood as potential "must have" criteria rather than something you'd take because they might make your performance numbers better.
I think there's a lot of room to assess performance for solving specific problems within these categories but it's a mistake to compare implementations or algorithms against alternatives which don't meet the criterion at all.
9
u/0x6675636B796F75 May 28 '26
As someone going through something similar at work writing a lock-free LRU cache where all read operations are wait-free. a few suggestions to look into if you haven't already for this implementation you're working on:
alignas()will be a good one to look up if you haven't tried applying it anywhere in these designs being tested.yield()calls when a thread will be blocked or has been looping on some CAS operation for too long - ideally leading to the blocking thread(s) to finish their work faster in the situations where they're actively blocking.This stuff is always fun the deeper you get into it all. It's one of the more interesting rabbit holes when you're really getting into micro-optimizations (most of what the above would probably be considered - but they absolutely add up to noticeably better performance when carefully taken into consideration in a way that best takes advantage of each concept).
9
u/matthieum May 27 '26
We absolutely can.
There are definitely lock-free queues without CAS:
And there are MP/MC designs without CAS too.
2
u/anotherprogrammer25 May 28 '26
Thanks for the article. Interesting read.
1
u/SparTV C++/Lua May 30 '26
You can also look at other metrics like cache missesz Google benchmark library has good measuring tools and PMU support
1
u/Ulrari Jun 06 '26
Good article! I have a few pieces of feedback: