goext/syncext/atomic.go
Mike Schwörer fa6d73301e
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 1m26s
v0.0.315 atomic
2023-11-12 03:10:55 +01:00

35 lines
416 B
Go

package syncext
import (
"sync"
)
type Atomic[T any] struct {
v T
lock sync.RWMutex
}
func NewAtomic[T any](value T) *Atomic[T] {
return &Atomic[T]{
v: value,
lock: sync.RWMutex{},
}
}
func (a *Atomic[T]) Get() T {
a.lock.RLock()
defer a.lock.RUnlock()
return a.v
}
func (a *Atomic[T]) Set(value T) T {
a.lock.Lock()
defer a.lock.Unlock()
oldValue := a.v
a.v = value
return oldValue
}