[concurrency-interest] atomicity and volatile

Dhanji R. Prasanna dhanji at gmail.com
Wed Mar 5 19:09:02 EST 2008

Hi I have a volatile field being incremented by multiple threads:
public class MyCounter {
    private volatile int count;

    public void increment() {


Am I correct in assuming that multiple threads could clobber each others
increments since count++ is not an atomic operation relative to all threads
calling increment? Even though the value of count is coherent between
threads when it is read...

So for instance:

thread A enters increment(), reads count as 0
MEANWHILE thread B enters increment(), also reads 0 in count (since thread A
has not yet set count to 1) and writes 1 to count.
thread A overwrites the value in count (which is already 1) with 1.

Therefore thread A clobbers thread B's increment work. Does that make sense?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20080306/876f0822/attachment.html 

More information about the Concurrency-interest mailing list