This package just provides with additional primitives to available in sync/atomic
.
Right now it provides with only 4 functions:
LoadMap
, which works similar toatomic.LoadUint64
, but for amap
, instead ofuint64
.StoreMap
, which works similar toatomic.StoreUint64
, but for amap
, instead ofuint64
.LoadPointer
, which works similar toatomic.LoadPointer
, but avoids necessity to manually work with theunsafe
package.StorePointer
, which works similar toatomic.StorePointer
, but avoids necessity to manually work with theunsafe
package.
import "github.com/go-ng/xatomic"
...
var m *myStruct
...
go func(){
...
xatomic.StorePointer(&m, &myStruct{Err: myErr})
...
}()
go func(){
...
fmt.Println(xatomic.LoadPointer(&m).Err.Error())
...
}()
import "github.com/go-ng/xatomic"
...
var m map[string]any{}
...
go func(){
...
xatomic.StoreMap(m, myMap)
...
}()
go func(){
...
myMap := xatomic.LoadMap(m)
...
}()
It uses Go generics, so the type of the map is preserved after StoreMap
& LoadMap
.
goos: linux
goarch: amd64
pkg: github.com/go-ng/xatomic
cpu: 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
Benchmark/noop-16 1000000000 0.2236 ns/op
Benchmark/Map/Store/atomic-16 987858870 6.108 ns/op
Benchmark/Map/Store/unatomic-16 1000000000 0.3973 ns/op
Benchmark/Map/Load/atomic-16 1000000000 0.4589 ns/op
Benchmark/Map/Load/unatomic-16 1000000000 0.4611 ns/op
PASS
ok github.com/go-ng/xatomic 8.364s