https://github.com/alecthomas/atomic
Type-safe atomic values for Go
https://github.com/alecthomas/atomic
Last synced: about 1 year ago
JSON representation
Type-safe atomic values for Go
- Host: GitHub
- URL: https://github.com/alecthomas/atomic
- Owner: alecthomas
- License: mit
- Created: 2021-12-04T02:18:41.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-12-02T11:37:40.000Z (over 2 years ago)
- Last Synced: 2025-04-02T08:35:59.330Z (over 1 year ago)
- Language: Go
- Size: 9.77 KB
- Stars: 19
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: COPYING
Awesome Lists containing this project
README
# Type-safe atomic values for Go
One issue with Go's sync/atomic package is that there is no guarantee from the
type system that operations on an integer value will be applied through the
sync/atomic functions. This package solves that and introduces two type-safe
interfaces for use with integer and non-integer atomic values.
The first interface is for any value:
```go
// Interface represents a value that can be atomically loaded or replaced.
type Interface[T any] interface {
// Load value atomically.
Load() T
// Store value atomically.
Store(value T)
// Swap the previous value with the new value atomically.
Swap(new T) (old T)
// CompareAndSwap the previous value with new if its value is "old".
CompareAndSwap(old, new T) (swapped bool)
}
```
The second interface is a `Value[T]` constrained to the 32 and 64 bit integer
types and adds a single `Add()` method:
```go
// Int expresses atomic operations on signed or unsigned integer values.
type Int[T atomicint] interface {
Value[T]
// Add a value and return the new result.
Add(delta T) (new T)
}
```
## Performance
```
BenchmarkInt64Add
BenchmarkInt64Add-8 174217112 6.887 ns/op
BenchmarkIntInterfaceAdd
BenchmarkIntInterfaceAdd-8 174129980 6.889 ns/op
BenchmarkStdlibInt64Add
BenchmarkStdlibInt64Add-8 174152660 6.887 ns/op
BenchmarkInterfaceStore
BenchmarkInterfaceStore-8 16015668 76.17 ns/op
BenchmarkValueStore
BenchmarkValueStore-8 16155405 75.03 ns/op
BenchmarkStdlibValueStore
BenchmarkStdlibValueStore-8 16391035 74.85 ns/op
```