← InterviewExpert
Will the Other Thread See What I Wrote?
Thread A writes a struct and then sets a flag. Thread B sees the flag and reads the struct. What can go wrong?
The scenario
A C++ service publishes a config object:
cfg = parse(); ready = true; with both plain variables, and readers spin on ready. It has run for two years on x86 and started segfaulting weekly after a migration to ARM instances.What it tests
- Whether the candidate knows program order is not observation order
- Whether they can name happens-before as the thing that has to be established
- Whether they understand that both compilers and CPUs reorder, and that hardware differs
Answer it out loud first. Recognising a good answer is not the same skill as producing one.