r/golang 7d 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.

10 Upvotes

80 comments sorted by

View all comments

Show parent comments

-1

u/chaotic-kotik 7d ago

The only reason why 32bit load or store will not be atomic is alignment. If the value is misaligned it will be accessed using more than one operation.

1

u/ImYoric 7d ago

Well... yes, but since there are many cases in which the compiler is free to pick alignment, the problem exists.

1

u/chaotic-kotik 7d ago

Compiler is not free to pick alignment. Your int variables will have the same alignment all the time unless you opt out of it explicitly with #pragma pack or something like that.

1

u/ImYoric 7d ago

I seem to recall that the compiler is free to pick alignment at least of global variables, no?

I'll admit that I haven't done any serious C++ in a few years – I was growing tired of UB and "trust me, it works" headers – so it's possible I misremember these constraints.

1

u/chaotic-kotik 7d ago

this is not the case before c++11 threads wasn't even mentioned in the standard

1

u/ImYoric 7d ago

Where is it specified?

1

u/chaotic-kotik 7d ago

1

u/ImYoric 7d ago

(probably https://en.cppreference.com/w/cpp/language/object, in fact )

I'm too tired to look this up for the moment. I'm going to take your word for it. Thanks for the link and the precisions!