added Atom[T] to have atomic manipulation of composite values

This commit is contained in:
2025-03-13 21:24:59 +09:00
parent 4d3fb7db65
commit cd32380425
6 changed files with 135 additions and 95 deletions

21
atom.go Normal file
View File

@ -0,0 +1,21 @@
package hodu
import "sync/atomic"
type Atom[T any] struct {
val atomic.Value
}
func (av* Atom[T]) Set(v T) {
av.val.Store(v)
}
func (av* Atom[T]) Get() T {
var v interface{}
v = av.val.Load()
if v == nil {
var t T
return t // return the zero-value
}
return v.(T)
}