Documentation
¶
Overview ¶
Package mutex provides mutexes that wrap the thing they protect. This results in clearer code than using sync.Mutex directly.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Locked ¶
type Locked[T any] struct { // contains filtered or unexported fields }
A Locked[T] is a reference to a value of type T which is guarded by a Mutex, which the caller has acquired.
func (*Locked[T]) Unlock ¶
func (l *Locked[T]) Unlock()
Unlock releases the mutex. Any references to the value obtained via Value must not be used after this is called.
func (*Locked[T]) Value ¶
func (l *Locked[T]) Value() *T
Value returns a reference to the protected value. It must not be used after calling Unlock.
Value will panic if called after Unlock()
We recommend against assigning the result of Value() to a variable; while repeatedly having to write l.Value() is mildly annoying, it makes it much harder to accidentally use the value after unlocking.
type Mutex ¶
type Mutex[T any] struct { // contains filtered or unexported fields }
A Mutex[T] wraps a T, protecting it with a mutex. It must not be moved after first use. The zero value is an unlocked mutex containing the zero value of T.
func (*Mutex[T]) Lock ¶
Lock acquires the mutex and returns a reference to the value. Call Unlock() on the return value to release the lock.
Where possible, you should prefer using Mutex.With and similar functions. If those are insufficient to handle your use case consider whether your locking scheme is too complicated before going ahead and using this.