Question discussion

Two threads each execute the loop "for (i = 0; i < 100; i++) counter = counter + 1;" on a shared variable counter that is initially 0. The…

2328Medium DSSSB TGT Computer Science 0 replies 1 views
0

Two threads each execute the loop "for (i = 0; i < 100; i++) counter = counter + 1;" on a shared variable counter that is initially 0. The statement is compiled as load, add and store, and the threads are not synchronised. What is the minimum possible final value of counter?

  • A2
  • B1
  • C100
  • D0
Correct answer: A

Explanation

With a suitable interleaving of the loads and stores one thread's last update can overwrite the other's progress so that the final value is only 2 (a final value of 1 or 0 is impossible).

Discussion 0

Nobody has replied yet. Start the discussion!