Reputation: 455
We have 2 threads:
they have no task scheduling
they also have no synchronization mechanisms
Their task is:
to read a value into a register
increment the value in the register
write the value back to location X
At start X contains value 0. Both threads modify value at same location.
Both threads start at the same time and do 1000 iterations.
Question: What is the smallest value X after both threads finish? (not 1000 and not 2000)
Upvotes: 2
Views: 109
Reputation: 7529
The smallest possible number is 2.
T1 reads the value 0.
T2 increments X 999 times to 999.
T1 writes the value 1
T2 reads the value 1
T1 increments X 999 times to 1000
T2 writes the value 2.
Upvotes: 8