Using volatile long as an atomic

后端 未结 6 1888
感动是毒
感动是毒 2021-01-13 14:29

If I have something like this...

volatile long something_global = 0;

long some_public_func()
{
    return something_global++;
}

Would it b

6条回答
  •  感动是毒
    2021-01-13 15:27

    No - volatile does not mean synchronized. It just means that every access will return the most up-to-date value (as opposed to a copy cached locally in the thread).

    Post-increment is not an atomic operation, it is a memory access followed by a memory write. Interleaving two can mean that the value is actually incremented just once.

提交回复
热议问题