CS FundamentalsVery hardGATE CS

Shared int x = 0. Two threads each execute `x = x + 1;` TWICE, without synchronisation; each statement is a load into a private register, an add, and a store. What is the set of possible final values of x?

  1. A. {3, 4}
  2. B. {4}
  3. C. {2, 3, 4}
  4. D. {1, 2, 3, 4}

Correct answer: C. {2, 3, 4}

Final value 2 is possible: T1 loads 0, T2 runs its first increment (x = 1), T1 stores 1, T2 loads 1, T1 runs its second increment completely (x = 2), then T2 stores 2. A final value of 1 is impossible: the last store comes from a thread's second increment, whose load happens after that thread already stored at least 1. Enumerating all interleavings confirms {2, 3, 4}.

More Operating Systems questions

Exams that ask this topic