queue

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2021 License: GPL-3.0 Imports: 3 Imported by: 0

README

golang-queue

queue provides an efficient implementation of a multi-producer, single-consumer lock-free queue

Reference

MpscQueue: github.com/AsynkronIT/protoactor-go

Interface

type Queue interface {
    Push(v interface{}) bool
    Pop() interface{}
    Empty() bool
    Size() int64
}

usage

q := NewRingQueue(size)
q.Push(value)

//note: pop only be call from single goroutine
v = q.Pop()

benchmark

test env : i5 8th GEN / 16G
2000 goroutine * 10000 push && pop

RingQueue: 420ns/op
MpscQueue: 640ns/op
CQueue: 830ns/op

goos: linux
goarch: amd64
pkg: queue
BenchmarkRingqueue
BenchmarkRingqueue-8   	       1	8396128600 ns/op	697504768 B/op	20001304 allocs/op
BenchmarkMpscqueue
BenchmarkMpscqueue-8   	       1	12918583100 ns/op	800511184 B/op	40002378 allocs/op
BenchmarkCQueue
BenchmarkCQueue-8      	       1	16738020500 ns/op	800215496 B/op	40001946 allocs/op
PASS
coverage: 64.9% of statements
ok  	queue	38.523s

Documentation

Overview

Package mpsc provides an efficient implementation of a multi-producer, single-consumer lock-free queue.

The Push function is safe to call from multiple goroutines. The Pop and Empty APIs must only be called from a single, consumer goroutine.

The Push function is safe to call from multiple goroutines. The Pop and Empty APIs must only be called from a single, consumer goroutine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Roundup2

func Roundup2(v uint64) uint64

roundup to power of 2 for bitwise modulus: x % n == x & (n - 1).

Types

type CQueue

type CQueue struct {
	// contains filtered or unexported fields
}

CQueue is a concurrent unbounded queue which uses two-Lock concurrent queue qlgorithm.

func NewCQueue

func NewCQueue() *CQueue

NewCQueue returns an empty CQueue.

func (*CQueue) Pop

func (q *CQueue) Pop() interface{}

removes and returns the value at the head of the queue. It returns nil if the queue is empty.

func (*CQueue) Push

func (q *CQueue) Push(v interface{}) bool

puts the given value v at the tail of the queue.

type MpscQueue

type MpscQueue struct {
	// contains filtered or unexported fields
}

func NewMpscQueue

func NewMpscQueue() *MpscQueue

func (*MpscQueue) Empty

func (q *MpscQueue) Empty() bool

Empty returns true if the queue is empty

Empty must be called from a single, consumer goroutine

func (*MpscQueue) Peek

func (q *MpscQueue) Peek() interface{}

must be call from single consumer

func (*MpscQueue) Pop

func (q *MpscQueue) Pop() interface{}

Pop removes the item from the front of the queue or nil if the queue is empty

Pop must be called from a single, consumer goroutine

func (*MpscQueue) Push

func (q *MpscQueue) Push(x interface{}) bool

Push adds x to the back of the queue.

Push can be safely called from multiple goroutines

func (*MpscQueue) Size

func (q *MpscQueue) Size() int64

type Queue

type Queue interface {
	Push(v interface{}) bool
	Pop() interface{}
	Empty() bool
	Size() int64
}

Queue is a FIFO data structure. Push puts a value into its tail, Pop removes a value from its head.

type RingQueue

type RingQueue struct {
	// contains filtered or unexported fields
}

func NewRingQueue

func NewRingQueue(size int64) *RingQueue

func (*RingQueue) Empty

func (q *RingQueue) Empty() bool

func (*RingQueue) Peek

func (q *RingQueue) Peek() interface{}

must be call from single consumer

func (*RingQueue) Pop

func (q *RingQueue) Pop() interface{}

must be call from single consumer

func (*RingQueue) Push

func (q *RingQueue) Push(x interface{}) bool

can be safe to call from multiple goroutines, if q is full , return false

func (*RingQueue) Size

func (q *RingQueue) Size() int64

Jump to

Keyboard shortcuts

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