Documentation
¶
Overview ¶
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 conc.Chan or the facilities of the sync package.
Each type's zero value is a usable, zeroed atomic; no initialization is needed. All operations use sequentially consistent ordering. The types must not be copied after first use.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bool ¶
type Bool struct {
// contains filtered or unexported fields
}
Bool is an atomic boolean value. The zero value is false. Bool must not be copied after first use.
Example ¶
package main
import (
"solod.dev/so/sync/atomic"
)
func main() {
// A stop flag polled by a worker and set from another thread.
var stop atomic.Bool
if stop.Load() {
println("stop requested")
}
stop.Store(true)
if stop.Load() {
println("stop requested")
}
// stop requested
}
Output:
func (*Bool) CompareAndSwap ¶
CompareAndSwap atomically sets x to new if it currently holds old, reporting whether the swap happened.
type Int32 ¶
type Int32 struct {
// contains filtered or unexported fields
}
Int32 is an atomic int32. The zero value is zero. Int32 must not be copied after first use.
func (*Int32) CompareAndSwap ¶
CompareAndSwap atomically sets x to new if it currently holds old, reporting whether the swap happened.
type Int64 ¶
type Int64 struct {
// contains filtered or unexported fields
}
Int64 is an atomic int64. The zero value is zero. Int64 must not be copied after first use.
Example ¶
package main
import (
"solod.dev/so/conc"
"solod.dev/so/mem"
"solod.dev/so/sync/atomic"
)
func bump(arg any) {
cnt := arg.(*atomic.Int64)
cnt.Add(1)
}
func main() {
// A shared counter incremented by many threads without a mutex.
var cnt atomic.Int64
opts := conc.PoolOptions{NumThreads: 4}
pool := conc.NewPool(mem.System, opts)
defer pool.Free()
for range 100 {
pool.Go(bump, &cnt)
}
pool.Wait()
println(cnt.Load())
// 100
}
Output:
func (*Int64) CompareAndSwap ¶
CompareAndSwap atomically sets x to new if it currently holds old, reporting whether the swap happened.
type Pointer ¶
type Pointer[T any] struct { // contains filtered or unexported fields }
Pointer is an atomic pointer of type *T. The zero value is a nil *T. Pointer must not be copied after first use.
Example ¶
package main
import (
"solod.dev/so/sync/atomic"
)
type config struct {
addr string
}
func main() {
// Publishing a new config that readers pick up atomically.
var cur atomic.Pointer[config]
old := config{addr: "localhost:8080"}
cur.Store(&old)
next := config{addr: "localhost:9090"}
if cur.CompareAndSwap(&old, &next) {
println(cur.Load().addr)
}
// localhost:9090
}
Output:
func (*Pointer[T]) CompareAndSwap ¶
CompareAndSwap atomically sets x to new if it currently holds old, reporting whether the swap happened.
type Uint32 ¶
type Uint32 struct {
// contains filtered or unexported fields
}
Uint32 is an atomic uint32. The zero value is zero. Uint32 must not be copied after first use.
func (*Uint32) Add ¶
Add atomically adds delta to x and returns the new value. To subtract, add the two's complement of the operand (x.Add(^(x-1))).
func (*Uint32) CompareAndSwap ¶
CompareAndSwap atomically sets x to new if it currently holds old, reporting whether the swap happened.
type Uint64 ¶
type Uint64 struct {
// contains filtered or unexported fields
}
Uint64 is an atomic uint64. The zero value is zero. Uint64 must not be copied after first use.
func (*Uint64) Add ¶
Add atomically adds delta to x and returns the new value. To subtract, add the two's complement of the operand (x.Add(^(x-1))).
func (*Uint64) CompareAndSwap ¶
CompareAndSwap atomically sets x to new if it currently holds old, reporting whether the swap happened.