lockfree

package
v0.0.0-...-d1ba03b Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Lock-free data structures

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

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

Queue is a lock-free queue with a single consumer and multiple producers.

func NewQueue

func NewQueue[T any]() *Queue[T]

Constructs a new empty queue with a head node of type T and initializes the tail to point to the head, enabling thread-safe operations.

func (*Queue[T]) Pop

func (q *Queue[T]) Pop() (val T, ok bool)

Returns the front element of the queue by updating the head to the next node, returning the value and true if the queue was non-empty, or the zero value and false otherwise.

func (*Queue[T]) Push

func (q *Queue[T]) Push(v T)

Adds the given value to the end of the queue using a thread-safe, lock-free algorithm with atomic compare-and-swap operations.

type YiQueue

type YiQueue[T any] struct {
	Notify chan struct{}
	// contains filtered or unexported fields
}

YiQueue is a lock-free Yielding Queue.

It is desgined to be used by a single consumer and multiple producers. Very little spin-locking is used; instead the ring will notify the consumer with a channel when the write rate is not keeping up with the read rate.

func NewYiQueue

func NewYiQueue[T any]() *YiQueue[T]

Constructs a new YiQueue with a buffered notification channel and an underlying queue for elements of type T.

func (*YiQueue[T]) Iter

func (yq *YiQueue[T]) Iter() iter.Seq[T]

Returns an iterator that sequentially pops elements from the YiQueue, yielding each value until the queue is empty or the yield function returns false.

func (*YiQueue[T]) Pop

func (yq *YiQueue[T]) Pop() (val T, ok bool)

Removes and returns the next element from the queue, spinning until a value is available if initially not present, or returns false if the queue is empty.

func (*YiQueue[T]) Push

func (yq *YiQueue[T]) Push(v T)

Adds an element to the YiQueue and non-blockingly notifies a consumer via a channel if this element is the first in the queue.

Jump to

Keyboard shortcuts

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