Machine-translated by Google. Questions are authored in English.
Top

Discussion

Two tasks each execute this on a shared counter, with no protection.

int t = count;
t = t + 1;
count = t;

Starting from zero, the final value may be

  • A.0 only, the two updates cancelling
  • B.3, the counter being incremented once too often
  • C.1 or 2, depending on the interleaving
  • D.2 only, both increments always taking effect

Answer: C

If either task completes all three lines before the other begins, the answer is two; if both read before either writes, one increment is lost and the answer is one. That the answer is not determinate is the whole meaning of a race.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.