concurrent

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MutexValue

type MutexValue[T any] struct {
	// contains filtered or unexported fields
}

MutexValue holds a value of any type T with a mutex for safe concurrent access. All operations on the value are protected by the mutex.

func NewMutexValue

func NewMutexValue[T any](v T) *MutexValue[T]

func (*MutexValue[T]) Get

func (m *MutexValue[T]) Get() T

Get returns a copy of the current value safely. It locks the mutex during the operation.

func (*MutexValue[T]) Set

func (m *MutexValue[T]) Set(v T)

Set updates the value safely by locking the mutex. It replaces the current value with the new one.

func (*MutexValue[T]) WithLock

func (m *MutexValue[T]) WithLock(f func(*T))

WithLock executes the given function while holding the lock. The function receives a pointer to the value for in-place modification.

type RWMutexValue

type RWMutexValue[T any] struct {
	// contains filtered or unexported fields
}

RWMutexValue is a generic wrapper that pairs a value of type T with an RWMutex to provide thread-safe read and write access.

func NewRWMutexValue

func NewRWMutexValue[T any](v T) *RWMutexValue[T]

func (*RWMutexValue[T]) Get

func (m *RWMutexValue[T]) Get() T

Get returns a copy of the underlying value v. It acquires a read lock to ensure safe concurrent access.

func (*RWMutexValue[T]) Set

func (m *RWMutexValue[T]) Set(v T)

Set updates the underlying value v to the new value provided. It acquires a write lock to ensure exclusive access during the update.

func (*RWMutexValue[T]) WithLock

func (m *RWMutexValue[T]) WithLock(f func(*T))

WithLock executes the provided function f while holding a write lock. f is given a pointer to the underlying value v, allowing safe modifications. The lock is held for the duration of f to ensure exclusive access.

func (*RWMutexValue[T]) WithRLock

func (m *RWMutexValue[T]) WithRLock(f func(T))

WithRLock executes the provided function f while holding a read lock. f is given a copy of the underlying value v, ensuring that any modifications made within f do not affect the actual stored value. This method is useful for performing compound read operations without incurring the cost of multiple Get calls.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL