Skip to content

Latest commit

 

History

History
75 lines (59 loc) · 2.33 KB

README.md

File metadata and controls

75 lines (59 loc) · 2.33 KB

xatomic

GoDoc

About

This package just provides with additional primitives to available in sync/atomic.

Right now it provides with only 4 functions:

Examples

Pointer

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())
        ...
    }()

Slice

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.

Performance

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