Documentation
¶
Overview ¶
Package queue implements a lock-free concurrent FIFO queue using pre-allocated nodes. The queue is designed for high-performance concurrent access without locks, making it suitable for multi-producer, multi-consumer scenarios.
The queue pre-allocates a fixed number of nodes (65536 by default) for better memory locality and reduced allocation overhead. When the queue is full, attempting to enqueue will panic.
Example usage:
func Example() { // Create a queue with default options q1 := queue.New[string]() defer q1.Close() // Create a queue with custom options q2 := queue.New[string]( queue.WithMaxNodes(1<<20), // Set max nodes to 1 million queue.WithReclaimInterval(time.Second), // Reclaim nodes every second ) defer q2.Close() // Basic operations remain the same q2.Enqueue("first") q2.Enqueue("second") if val, ok := q2.Dequeue(); ok { fmt.Println(val) // Prints: first } if !q2.Empty() { val, _ := q2.Dequeue() fmt.Println(val) // Prints: second } }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Node ¶
type Node[T any] struct { // contains filtered or unexported fields }
Node represents a node in the queue.
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue represents a concurrent FIFO queue with pre-allocated nodes.
func New ¶
func New[T any](opts ...QueueOption[T]) *Queue[T]
New creates a new empty queue with pre-allocated nodes.
type QueueOption ¶
QueueOption is a functional option for configuring a Queue.
func WithReclaimInterval ¶
func WithReclaimInterval[T any](interval time.Duration) QueueOption[T]
WithReclaimInterval sets the interval between reclamation runs.