logo       

Re: volatile long: msg#00028

java.jsr.166-concurrency

Subject: Re: volatile long

Hanson Char wrote:
> Thanks Joe, I did some experiment and the result is on your side. The
> ++count is a read-modify-write operation, not a pure write operation.
> So volatile will never work with such construct.
>
> Hanson

Yup, volatile only makes the reads and writes atomic, not combinations
of reads and writes. And ++ is definitely a read and a write (how else
will it know the value to increment?). In case you're interested,
here's an example execution of how you can get count to be negative:

count initially 0

Thread 1 Thread 2

read count (0)
read count (0)
write count = 1
write count = 1
read count (1)
write count = 0
read count (0)
write count = -1


- Victor


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise