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

Discussion

Which one of the following statements about two threads writing the same non-volatile int is true?

  • A.Two threads writing the same non-volatile int without synchronization may lose an update.
  • B.Two threads writing the same non-volatile int are safe.
  • C.Two threads writing the same non-volatile int cause a compile-time error.
  • D.Two threads writing the same non-volatile int always produce the larger of the two values.

Answer: A

Atomicity of a single write is not the issue: each write lands whole, but without ordering or visibility guarantees one thread can overwrite the other's value or read a stale one, so an update disappears. The write being atomic is exactly why this looks safe and is not.

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

Post your comment

Please log in or register to post a comment.