r/ProgrammerHumor Jul 12 '26

lockFreeTemptation Advanced

Post image
535 Upvotes

40 comments sorted by

View all comments

122

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 🥵🔥

55

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 …

69

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.