I have two threads, one updating an int and one reading it. This is a statistic value where the order of the reads and writes is irrelevant.
My question is, do I need to synchronize access to this multi-byte value anyway? Or, put another way, can part of the write be complete and get interrupted, and then the read happen.
For example, think of a value = 0x0000FFFF that gets incremented value of 0x00010000.
Is there a time where the value looks like 0x0001FFFF that I should be worried about? Certainly the larger the type, the more possible something like this to happen.
I've always synchronized these types of accesses, but was curious what the community thinks.
=
: #8291268 – Fendersonstd::atomic<int>
(perhaps withmemory_order_relaxed
) to get the compiler to make the asm you want. That part is true on all ISAs. – Piccaninnyint64_t
(unlike withatomic<>
orvolatile
) for a simple assignment. That's probably a better choice as a duplicate target for questions about whetherint
"is atomic", since it's not just 64-bit types – Piccaninny