Reputation: 129
As go's document illuminate:
The add operation, implemented by the AddT functions, is the atomic equivalent of:
*addr += delta
return *addr
The load and store operations, implemented by the LoadT and StoreT functions, are the atomic equivalents of "return *addr" and "*addr = val".
Here's my question:
Why should i use 'atomic.AddT' instead of '+'?
Why 'atomic.LoadT' not '=' ?
Isn't '=' and '+' aomic?
Can anyone give me an example that show differences between them?
Upvotes: 0
Views: 1066
Reputation: 51582
First, you need to know about the go memory model:
The crucial point to understand is that the effects of variable writes in a goroutine are not necessarily visible to another goroutine in the same way the writing goroutine observes them. That is, if a goroutine writes a value to a variable a
and then to another variable b
, another goroutine may see these writes in an unspecified order. That's why you need concurrent primitives like channels or mutexes. Anything that happened before a channel operation or mutex operation will be visible to all goroutines at that point.
Atomic operations provide similar guarantees although the memory model doesn't explicitly specify that.
It is not easy to write code using atomics. You have to consider all possible execution interleavings to make sure there are no races. Stick with channels and mutexes. Correct code is better than fast but racy code.
Upvotes: 2
Reputation: 42431
You simply should not use package atomic at all.
As the package documentation clearly states
Package atomic provides low-level atomic memory primitives useful for implementing synchronization algorithms.
These functions require great care to be used correctly. Except for special, low-level applications, synchronization is better done with channels or the facilities of the sync package. Share memory by communicating; don't communicate by sharing memory.
It is intended for experts only.
You asked: "Isn't '=' and '+' aomic?" No! Nothing (!) in Go is atomic, except the primitives in package atomic (hence the name), what package sync provides and channel operations.
So no, you cannot use =
, +
, -
, /
, etc. operating concurrently. And you literally never should write code that assumes something to be atomic to be correct. Always provide correct synchronisation (via package sync) or via channels.
Upvotes: -3
Reputation: 365
Imagine, there are 5 goroutines accessing the same memory/source initialized at 0 at the same time and trying to increment it. A regular increment "+" operation would lead a data race as there's no guarantee of the state being updated correctly due to simultaneous operations. In the end, there is no guarantee that value of the shared variable is 5
Atomic operations helps in this case as they lock the memory for safe operations by goroutines and hence no data races is observed
You can also use channels or mutexes to lock state and operate safely which is more recommended
Upvotes: 1