package atomic
import "go.uber.org/atomic"
Package atomic provides simple wrappers around numerics to enforce atomic
access.
Code:play
Output:Example¶
package main
import (
"fmt"
"go.uber.org/atomic"
)
func main() {
// Uint32 is a thin wrapper around the primitive uint32 type.
var atom atomic.Uint32
// The wrapper ensures that all operations are atomic.
atom.Store(42)
fmt.Println(atom.Inc())
fmt.Println(atom.CAS(43, 0))
fmt.Println(atom.Load())
}
43
true
0
Index ¶
- type Bool
- func NewBool(initial bool) *Bool
- func (b *Bool) CAS(old, new bool) bool
- func (b *Bool) Load() bool
- func (b *Bool) Store(new bool)
- func (b *Bool) Swap(new bool) bool
- func (b *Bool) Toggle() bool
- type Float64
- func NewFloat64(f float64) *Float64
- func (f *Float64) Add(s float64) float64
- func (f *Float64) CAS(old, new float64) bool
- func (f *Float64) Load() float64
- func (f *Float64) Store(s float64)
- func (f *Float64) Sub(s float64) float64
- type Int32
- func NewInt32(i int32) *Int32
- func (i *Int32) Add(n int32) int32
- func (i *Int32) CAS(old, new int32) bool
- func (i *Int32) Dec() int32
- func (i *Int32) Inc() int32
- func (i *Int32) Load() int32
- func (i *Int32) Store(n int32)
- func (i *Int32) Sub(n int32) int32
- func (i *Int32) Swap(n int32) int32
- type Int64
- func NewInt64(i int64) *Int64
- func (i *Int64) Add(n int64) int64
- func (i *Int64) CAS(old, new int64) bool
- func (i *Int64) Dec() int64
- func (i *Int64) Inc() int64
- func (i *Int64) Load() int64
- func (i *Int64) Store(n int64)
- func (i *Int64) Sub(n int64) int64
- func (i *Int64) Swap(n int64) int64
- type String
- func NewString(str string) *String
- func (s *String) Load() string
- func (s *String) Store(str string)
- type Uint32
- func NewUint32(i uint32) *Uint32
- func (i *Uint32) Add(n uint32) uint32
- func (i *Uint32) CAS(old, new uint32) bool
- func (i *Uint32) Dec() uint32
- func (i *Uint32) Inc() uint32
- func (i *Uint32) Load() uint32
- func (i *Uint32) Store(n uint32)
- func (i *Uint32) Sub(n uint32) uint32
- func (i *Uint32) Swap(n uint32) uint32
- type Uint64
- func NewUint64(i uint64) *Uint64
- func (i *Uint64) Add(n uint64) uint64
- func (i *Uint64) CAS(old, new uint64) bool
- func (i *Uint64) Dec() uint64
- func (i *Uint64) Inc() uint64
- func (i *Uint64) Load() uint64
- func (i *Uint64) Store(n uint64)
- func (i *Uint64) Sub(n uint64) uint64
- func (i *Uint64) Swap(n uint64) uint64
- type Value
Examples ¶
Types ¶
type Bool ¶
type Bool struct {
// contains filtered or unexported fields
}
Bool is an atomic Boolean.
func NewBool ¶
NewBool creates a Bool.
func (*Bool) CAS ¶
CAS is an atomic compare-and-swap.
func (*Bool) Load ¶
Load atomically loads the Boolean.
func (*Bool) Store ¶
Store atomically stores the passed value.
func (*Bool) Swap ¶
Swap sets the given value and returns the previous value.
func (*Bool) Toggle ¶
Toggle atomically negates the Boolean and returns the previous value.
type Float64 ¶
type Float64 struct {
// contains filtered or unexported fields
}
Float64 is an atomic wrapper around float64.
func NewFloat64 ¶
NewFloat64 creates a Float64.
func (*Float64) Add ¶
Add atomically adds to the wrapped float64 and returns the new value.
func (*Float64) CAS ¶
CAS is an atomic compare-and-swap.
func (*Float64) Load ¶
Load atomically loads the wrapped value.
func (*Float64) Store ¶
Store atomically stores the passed value.
func (*Float64) Sub ¶
Sub atomically subtracts from the wrapped float64 and returns the new value.
type Int32 ¶
type Int32 struct {
// contains filtered or unexported fields
}
Int32 is an atomic wrapper around an int32.
func NewInt32 ¶
NewInt32 creates an Int32.
func (*Int32) Add ¶
Add atomically adds to the wrapped int32 and returns the new value.
func (*Int32) CAS ¶
CAS is an atomic compare-and-swap.
func (*Int32) Dec ¶
Dec atomically decrements the wrapped int32 and returns the new value.
func (*Int32) Inc ¶
Inc atomically increments the wrapped int32 and returns the new value.
func (*Int32) Load ¶
Load atomically loads the wrapped value.
func (*Int32) Store ¶
Store atomically stores the passed value.
func (*Int32) Sub ¶
Sub atomically subtracts from the wrapped int32 and returns the new value.
func (*Int32) Swap ¶
Swap atomically swaps the wrapped int32 and returns the old value.
type Int64 ¶
type Int64 struct {
// contains filtered or unexported fields
}
Int64 is an atomic wrapper around an int64.
func NewInt64 ¶
NewInt64 creates an Int64.
func (*Int64) Add ¶
Add atomically adds to the wrapped int64 and returns the new value.
func (*Int64) CAS ¶
CAS is an atomic compare-and-swap.
func (*Int64) Dec ¶
Dec atomically decrements the wrapped int64 and returns the new value.
func (*Int64) Inc ¶
Inc atomically increments the wrapped int64 and returns the new value.
func (*Int64) Load ¶
Load atomically loads the wrapped value.
func (*Int64) Store ¶
Store atomically stores the passed value.
func (*Int64) Sub ¶
Sub atomically subtracts from the wrapped int64 and returns the new value.
func (*Int64) Swap ¶
Swap atomically swaps the wrapped int64 and returns the old value.
type String ¶
type String struct {
// contains filtered or unexported fields
}
String is an atomic type-safe wrapper around Value for strings.
func NewString ¶
NewString creates a String.
func (*String) Load ¶
Load atomically loads the wrapped string.
func (*String) Store ¶
Store atomically stores the passed string. Note: Converting the string to an interface{} to store in the Value requires an allocation.
type Uint32 ¶
type Uint32 struct {
// contains filtered or unexported fields
}
Uint32 is an atomic wrapper around an uint32.
func NewUint32 ¶
NewUint32 creates a Uint32.
func (*Uint32) Add ¶
Add atomically adds to the wrapped uint32 and returns the new value.
func (*Uint32) CAS ¶
CAS is an atomic compare-and-swap.
func (*Uint32) Dec ¶
Dec atomically decrements the wrapped int32 and returns the new value.
func (*Uint32) Inc ¶
Inc atomically increments the wrapped uint32 and returns the new value.
func (*Uint32) Load ¶
Load atomically loads the wrapped value.
func (*Uint32) Store ¶
Store atomically stores the passed value.
func (*Uint32) Sub ¶
Sub atomically subtracts from the wrapped uint32 and returns the new value.
func (*Uint32) Swap ¶
Swap atomically swaps the wrapped uint32 and returns the old value.
type Uint64 ¶
type Uint64 struct {
// contains filtered or unexported fields
}
Uint64 is an atomic wrapper around a uint64.
func NewUint64 ¶
NewUint64 creates a Uint64.
func (*Uint64) Add ¶
Add atomically adds to the wrapped uint64 and returns the new value.
func (*Uint64) CAS ¶
CAS is an atomic compare-and-swap.
func (*Uint64) Dec ¶
Dec atomically decrements the wrapped uint64 and returns the new value.
func (*Uint64) Inc ¶
Inc atomically increments the wrapped uint64 and returns the new value.
func (*Uint64) Load ¶
Load atomically loads the wrapped value.
func (*Uint64) Store ¶
Store atomically stores the passed value.
func (*Uint64) Sub ¶
Sub atomically subtracts from the wrapped uint64 and returns the new value.
func (*Uint64) Swap ¶
Swap atomically swaps the wrapped uint64 and returns the old value.
type Value ¶
Value shadows the type of the same name from sync/atomic https://godoc.org/sync/atomic#Value
Source Files ¶
atomic.go string.go
- Version
- v1.3.0
- Published
- Aug 29, 2017
- Platform
- windows/amd64
- Imports
- 2 packages
- Last checked
- now –
Tools for package owners.