If, as you said in your editing, you only want to convince you of the collapse, then you do not need to do anything (at least, as a rule). If you encounter conflicts between threads, the worst thing that happens is that the data will be damaged - for example, the reader may receive a value that was partially updated and does not directly correspond to any value that the thread wrote. A classic example would be multibyte the number you added something to and was carried over (for example), the old value was 0x3f ffff, which was increasing. Perhaps the read stream can see 0x3f 0000, where the lower 16 bits were increased, but the transfer to the upper 16 bits has not yet occurred.
On a modern machine, the increment on this small data element will usually be atomic, but there will be some size (and alignment) where it is absent - usually if part of the variable is on one line of the cache and partially on another, it will no longer be atomic. The exact size and alignment for this varies somewhat, but the basic idea remains the same - basically it's a matter of quantity having enough numbers to make this happen.
Of course, if you are not careful, something like this can block your code or something in that order - it's impossible to guess what could happen without knowing anything about how you plan to use the data.
source share