If I have something like this…
volatile long something_global = 0;
long some_public_func()
{
return something_global++;
}
Would it be reasonable to expect this code to not break (race condition) when accessed with multiple threads? If it’s not standard, could it still be done as a reasonable assumption about modern compilers?
NOTE: ALL I’m using this for is atomic increment and decrement – nothing fancier.
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.