[concurrency-interest] a question regarding non-volatile long

Holger Hoffstätte holger at wizards.de
Mon Aug 27 12:12:01 EDT 2007


Yechiel Feffer wrote:
> If, in multi-core 32 bits system, 2 threads are assigning values to a
> long variable concurrently (non volatile long)- is it possible that the
> final result will contain an inconsistent result- i.e. the first 32 bits
> part of the long variable will contain half of the long assigned by
> thread 1 while the second half will contain assignment from thread #2 ?

Strictly speaking the answer to your question is "yes". Volatile extends
the atomicity of reading/writing scalar values to longs and doubles. This
does not help with interleaved operations by multiple threads
(read-modify-write, aka long++), which still need to be synchronized properly.

Holger



More information about the Concurrency-interest mailing list