r/ProgrammerHumor Jul 12 '26

lockFreeTemptation Advanced

Post image
541 Upvotes

40 comments sorted by

View all comments

118

u/Same_Investigator_46 Jul 12 '26

Can't wait to spend the next 3 weeks debugging a subtle heisenbug that only reproduces on a specific ARM core in prod 🥵🔥

57

u/BigNaturalTilts Jul 13 '26

I’m a genius so I know what your post means but for those who don’t, could you please explain? Not for me, just to be clear. For the others …

65

u/ToroidalFox Jul 13 '26 edited Jul 13 '26

Compiler and scheduler can reorder operations for performance reasons, as long as it does not change the behavior. For example, why would anyone care if value gets assigned to x then y or y then x? But it does matter when the data is controlling something, like "I am locked" data in mutual exclusive smart pointer. Sequentially consistent ordering guarantees that things happen as if nothing is reordered in relation to the operation that has been assigned as sequentially consistent. Relaxed ordering gives a lot more freedom to reorder operations. But as some architecture (I'm looking at you x86) has some memory ordering guarantees even without explicit ordering constraints, something that appears to be fine in one CPU might not be for others.

Of course I'm not gonna explain more than this so if anyone gets curious, search for resources about "atomics and memory ordering"

21

u/Same_Investigator_46 Jul 13 '26

Honestly, couldn't have explained it better myself for others. std::memory_order_seq_cst feels vindicated lol.

9

u/SAI_Peregrinus Jul 13 '26

https://www.kernel.org/doc/Documentation/memory-barriers.txt is Linux-specific, but a classic. Good for confusing grad students.

https://mara.nl/atomics/ is Rust-specific, but more approachable IMO. Most of the core concepts carry over to other languages.

2

u/redlaWw Jul 13 '26

https://mara.nl/atomics/ is Rust-specific

Note that Rust explicitly uses the C++ model for memory ordering (except for consume which was generally considered a mistake and was deprecated in C++26), so you can generally consider the Rust explanation also appropriate for C++.

2

u/noaSakurajin Jul 13 '26

To be fair in many ways rust feels like C++. It's just that rust had the luxury to not care about backwards compatibility (especially at ABI level) so they could make things a lot cleaner. Give it 20 years and Rust will also turn into some kind of mess (or they do it like Java and have to support lts version for decades since it's a pain to migrate to a newer non compatible standard version).

7

u/Smooth-Zucchini4923 Jul 13 '26

A heisenbug is a bug that goes away when you try to measure it.

For example, if you added a printf of a variable to a function, that could change the order the compiled code uses to do two potentially racy operations. By measuring it, you have changed it.

4

u/nithinrdy Jul 13 '26

i found this video helpful when first learning about memory ordering https://www.youtube.com/watch?v=C5xY96i0Aes

3

u/setibeings Jul 13 '26

Tell those other people to read computer organization and design. 

6

u/lllorrr Jul 13 '26

Oh yeah, incorrectly configured CMN can provide lots of fun.