https://github.com/gobwas/atomicfloat
Atomic float utils
https://github.com/gobwas/atomicfloat
Last synced: 10 months ago
JSON representation
Atomic float utils
- Host: GitHub
- URL: https://github.com/gobwas/atomicfloat
- Owner: gobwas
- Created: 2017-07-17T12:36:27.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-07-17T12:53:19.000Z (over 8 years ago)
- Last Synced: 2025-01-24T09:42:32.469Z (12 months ago)
- Language: Go
- Size: 1.95 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Atomic Float for Go
## Overview
This atomic float64 is slower than implementations with `sync.Mutex` for atomic
counters, but can be faster for compare and swap cases.
See benchmarks for more info:
```bash
go test -run=none -bench=. -cpu=4,8,12
```
## Usage
```go
package main
import (
"fmt"
"time"
"github.com/gobwas/atomicfloat"
)
func main() {
f64 := atomicfloat.NewFloat64()
for i := 0; i < 100; i++ {
go f64.Add(1)
}
for i := 0; i < 58; i++ {
go f64.Add(-1)
}
// Let all goroutines complete.
<-time.After(time.Second)
fmt.Printf("%.2f", f64.Load()) // 42.00
}
```