CS FundamentalsVery hardGATE CS

Two threads run on different cores. Thread 1: `data = 42; ready = 1;` Thread 2: `while (!ready); print(data);` Both variables are plain (non-atomic) ints initialised to 0, and the hardware may reorder stores. What can thread 2 print?

  1. A. Always 42
  2. B. Possibly 0, because the store to ready may become visible before the store to data
  3. C. Nothing, the loop never exits
  4. D. A random garbage value other than 0 or 42

Correct answer: B. Possibly 0, because the store to ready may become visible before the store to data

Without memory barriers (or atomic release/acquire), weakly ordered CPUs and compilers may reorder the two stores, so thread 2 can see ready = 1 with stale data = 0. A release store and acquire load fix it.

More Operating Systems questions

Exams that ask this topic