In C++, say I have a variable of basic data type, like int counter
, that is used by many threads. In order to modify counter
, a thread must obtain a simple lock first. But I want the value to be readable at any time, whether it is locked or not.
When a thread reads counter
while some other thread is modifying it, do I have any guarantee to at least get either the pre-write or post-write value, rather than some corrupted value?
For example:
//counter == 10
counter += 5;
//counter == 15
Will all threads reading counter
around this time be guaranteed to at least read 10
or 15
? Instead of some strange value like -834289
.
If the answer is implementation specific, I'm using Visual Studio 2015.