Skip to content

Instantly share code, notes, and snippets.

@koonix
Last active October 5, 2024 17:33
Show Gist options
  • Save koonix/47b796897bb476ea59cbebea43f227c5 to your computer and use it in GitHub Desktop.
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.
package nuke
import "sync"
type Nuclear[T any] struct {
mu sync.RWMutex
v T
}
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