containers

package
v1.14.2 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package containers provides generic, concurrency-safe data structures.

Bag is a lock-free, append-only collection backed by an atomic linked list. AtomicMap is a mutex-protected map that avoids the per-element allocation overhead of sync.Map. The [mpmc] subpackage provides a bounded multiple-producer, multiple-consumer queue, and the [mpsc] subpackage provides a lock-free multiple-producer, single-consumer queue.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicMap

type AtomicMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

AtomicMap is a mutex-protected map safe for concurrent use. It serves as a lighter alternative to sync.Map for workloads where the extra per-element allocation that sync.Map requires is undesirable.

The zero value is ready to use; the underlying map is allocated lazily on the first call to AtomicMap.LoadOrStore.

func (*AtomicMap[K, V]) Clear

func (m *AtomicMap[K, V]) Clear()

Clear removes all elements from the map.

func (*AtomicMap[K, V]) Len added in v1.14.1

func (m *AtomicMap[K, V]) Len() int

Len returns the number of entries in the map.

func (*AtomicMap[K, V]) LoadOrStore

func (m *AtomicMap[K, V]) LoadOrStore(key K, value V) (V, bool)

LoadOrStore returns the existing value for key if present. Otherwise, it stores and returns the given value. The boolean result is true if the value was loaded, false if stored.

func (*AtomicMap[K, V]) Store added in v1.14.0

func (m *AtomicMap[K, V]) Store(key K, value V)

Store writes value at key within the internal map. If the key already exists, its value is overwritten with the new value. If the key does not exist, it is created and value is assigned.

func (*AtomicMap[K, V]) Unwrap added in v1.14.0

func (m *AtomicMap[K, V]) Unwrap() map[K]V

Unwrap returns the internal map that backs the AtomicMap. A nil value may be returned if the AtomicMap has not yet been initialized, or Unwrap is called after a Clear call.

Once the internal map has been unwrapped, methods on the AtomicMap instance are no longer safe.

type Bag

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

Bag is a lock-free, append-only collection. Values can be added concurrently via Bag.Add and drained via Bag.Seq, but individual elements cannot be indexed or removed.

Bag uses an atomic linked list internally, so all methods are safe for concurrent use. The zero value is ready to use.

func (*Bag[T]) Add

func (b *Bag[T]) Add(v ...T)

Add adds the provided values to the Bag.

func (*Bag[T]) Clear added in v1.14.0

func (b *Bag[T]) Clear()

Clear removes all elements from the Bag.

func (*Bag[T]) Seq

func (b *Bag[T]) Seq() iter.Seq[T]

Seq atomically removes all values from the Bag and returns an iterator that yields them in LIFO (most-recently-added-first) order. The returned iterator is single-use: on early break it resumes from the break point rather than restarting, and a fully consumed iterator yields nothing on subsequent calls.

Directories

Path Synopsis
Package mpmc provides a bounded, multiple-producer, multiple-consumer queue with optional automatic buffer growth.
Package mpmc provides a bounded, multiple-producer, multiple-consumer queue with optional automatic buffer growth.
Package mpsc provides a lock-free MPSC (multiple-producer, single-consumer) queue built on atomic linked-list operations.
Package mpsc provides a lock-free MPSC (multiple-producer, single-consumer) queue built on atomic linked-list operations.

Jump to

Keyboard shortcuts

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