Last active
January 8, 2025 00:14
-
-
Save koonix/47b796897bb476ea59cbebea43f227c5 to your computer and use it in GitHub Desktop.
Nuclear atomically stores values. The closest thing to this in the stdlib is atomic.Pointer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "sync" | |
type Nuclear[T any] struct { | |
mu sync.RWMutex | |
v T | |
} | |
func NewNuclear[T any](v T) *Nuclear[T] { | |
return &Nuclear{ | |
v: v, | |
} | |
} | |
func (n *Nuclear[T]) Store(v T) { | |
n.mu.Lock() | |
defer n.mu.Unlock() | |
n.v = v | |
} | |
func (n *Nuclear[T]) Load() T { | |
n.mu.RLock() | |
defer n.mu.RUnlock() | |
return n.v | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment