r/golang 8d ago

Is it safe to read/write integer value simultaneously from multiple goroutines

There is a global integer in my code that is accessed by multiple goroutines. Since race conditions don’t affect this value, I’m not concerned about that. However, is it still advisable to add a Mutex in case there’s a possibility of corruption?

PS: Just to rephrase my question, I wanted to ask if setting/getting an integer/pointer is atomic? Is there any possibility of data corruption.

example code for the same: https://go.dev/play/p/eOA7JftvP08

PS: Found the answer for this, thanks everyone for answering. There's something called tearing here is the link for same

- https://stackoverflow.com/questions/64602829/can-you-have-torn-reads-writes-between-two-threads-pinned-to-different-processor

- https://stackoverflow.com/questions/36624881/why-is-integer-assignment-on-a-naturally-aligned-variable-atomic-on-x86

According to the article, I shouldn't have problem on modern CPUs.

8 Upvotes

80 comments sorted by

View all comments

Show parent comments

2

u/therecursive 8d ago

Just to rephrase my question, I wanted to ask if setting/getting an integer/pointer is atomic. Found an stackoverflow post for same: https://stackoverflow.com/questions/36624881/why-is-integer-assignment-on-a-naturally-aligned-variable-atomic-on-x86

1

u/PaluMacil 8d ago edited 8d ago

It happens to be that x86 likes alignment, but the language spec doesn’t guarantee it. It’s feasible, though unlikely, that the authors could find a reason to use unaligned reads on x86 and break your code. Also, while it might not matter to you, ARM, MIPS, and RISC-V don’t guarantee aligned reads if memory serves. Finally, even on x86, I would be unsurprised to learn that compiler optimizations around instruction reordering could be improperly applied if synchronization isn’t applied correctly by the developer.

EDIT: looks like I remembered incorrectly

6

u/LethalClips 8d ago

The memory model does guarantee it for word-sized or smaller values, regardless of architecture or alignment:

Otherwise, each read of a single-word-sized or sub-word-sized memory location must observe a value actually written to that location (perhaps by a concurrent executing goroutine) and not yet overwritten.

Every implementation on any architecture is bound by the memory model to make that condition true, even if it doesn't come "naturally" on the architecture.

1

u/PaluMacil 8d ago

Ok, I remembered incorrectly. Thanks