threadsafe

package
v1.130.4 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package threadsafe solves a small but important design problem in concurrent Go code: how to write reusable, goroutine-safe data structures and helpers without hard-coding a concrete lock type everywhere.

Problem

Many concurrency-aware packages need only a tiny synchronization contract (`Lock`/`Unlock` or `RLock`/`RUnlock`), but directly depending on concrete types like sync.Mutex or sync.RWMutex makes code less composable and harder to test. A package-level interface abstraction allows callers and dependent packages to share thread-safety behavior while keeping implementation details flexible.

What This Package Provides

This package intentionally stays minimal and defines two lock interfaces:

  • Locker: the write-lock contract (`Lock` and `Unlock`), aliased from sync.Locker.
  • RLocker: the read-lock contract (`RLock` and `RUnlock`) used by read/write synchronization patterns.

These interfaces are intended to be embedded or referenced by concurrent containers and utility types that must be safely used across multiple goroutines.

Why It Matters

  • Decouples concurrency contracts from concrete lock implementations.
  • Improves reuse across packages that need the same minimal synchronization surface.
  • Simplifies testing and composition when custom lock wrappers are used.

Usage

See the examples in:

  • github.com/tecnickcom/gogen/pkg/tsmap
  • github.com/tecnickcom/gogen/pkg/tsslice

Those packages demonstrate how these interfaces are used to build practical, thread-safe containers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Locker

type Locker sync.Locker

Locker defines exclusive lock semantics with Lock and Unlock methods.

type RLocker

type RLocker interface {
	RLock()
	RUnlock()
}

RLocker defines shared read-lock semantics with RLock and RUnlock methods.

Directories

Path Synopsis
Package tsmap solves the common concurrency problem of safely operating on Go maps shared across multiple goroutines without repeatedly writing lock/unlock boilerplate at every call site.
Package tsmap solves the common concurrency problem of safely operating on Go maps shared across multiple goroutines without repeatedly writing lock/unlock boilerplate at every call site.
Package tsslice solves the common concurrency problem of safely operating on Go slices shared across multiple goroutines without repeating lock boilerplate around every access.
Package tsslice solves the common concurrency problem of safely operating on Go slices shared across multiple goroutines without repeating lock boilerplate around every access.

Jump to

Keyboard shortcuts

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