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

Discussion

Why do race conditions occur ?

(i) Values of memory locations replicated in registers during execution.
(ii) Content switches at arbitrary times during execution.
(iii) Threads can see stale memory values in registers.

  • A.Only (ii)
  • B.Only (iii)
  • C.(i) & (ii)
  • D.(i), (ii) & (iii)

Answer: D

All three are causes: (i), (ii) & (iii).

A race condition arises when the result of concurrent threads depends on the timing of their interleaving. Three facts combine to cause it:

  • a thread copies a memory value into a register, works on it there and writes it back later (i);
  • the scheduler can switch threads at arbitrary points, even between that read and that write (ii, where 'content switches' means context switches);
  • so another thread can be working on a stale copy of the value in its registers, and one update overwrites the other (iii).

The classic example is two threads each executing count++ and the count rising by only 1. Any option leaving out one of these misses part of the mechanism.

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

Post your comment

Please log in or register to post a comment.