All Projects → enriquebris → Goconcurrentqueue

enriquebris / Goconcurrentqueue

Licence: mit
Go concurrent-safe, goroutine-safe, thread-safe queue

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Goconcurrentqueue

Arq
Fast job queuing and RPC in python with asyncio and redis.
Stars: ✭ 695 (+447.24%)
Mutual labels:  concurrency, concurrent, queue
queueable
Convert streams to async ⌛ iterables ➰
Stars: ✭ 43 (-66.14%)
Mutual labels:  queue, concurrency
treap
A thread-safe, persistent Treap (tree + heap) for ordered key-value mapping and priority sorting.
Stars: ✭ 23 (-81.89%)
Mutual labels:  concurrency, concurrent
Zio
ZIO — A type-safe, composable library for async and concurrent programming in Scala
Stars: ✭ 3,167 (+2393.7%)
Mutual labels:  concurrency, concurrent
java-multithread
Códigos feitos para o curso de Multithreading com Java, no canal RinaldoDev do YouTube.
Stars: ✭ 24 (-81.1%)
Mutual labels:  concurrency, concurrent
think-async
🌿 Exploring cooperative concurrency primitives in Python
Stars: ✭ 178 (+40.16%)
Mutual labels:  queue, concurrency
concurrency-kit
🚄 Concurrency abstractions framework for Apple Platforms [Task, Atomic, Lock, Operation, etc.].
Stars: ✭ 17 (-86.61%)
Mutual labels:  concurrency, concurrent
YACLib
Yet Another Concurrency Library
Stars: ✭ 193 (+51.97%)
Mutual labels:  concurrency, concurrent
Libconcurrent
©️ Concurrent Programming Library (Coroutine) for C11
Stars: ✭ 335 (+163.78%)
Mutual labels:  concurrency, concurrent
Mpmcqueue
A bounded multi-producer multi-consumer concurrent queue written in C++11
Stars: ✭ 468 (+268.5%)
Mutual labels:  concurrency, queue
Hamsters.js
100% Vanilla Javascript Multithreading & Parallel Execution Library
Stars: ✭ 517 (+307.09%)
Mutual labels:  concurrency, concurrent
beems
a bee-queue based minimalist toolkit for building fast, decentralized, scalable and fault tolerant microservices
Stars: ✭ 33 (-74.02%)
Mutual labels:  queue, concurrency
practice
Java并发编程与高并发解决方案:http://coding.imooc.com/class/195.html Java开发企业级权限管理系统:http://coding.imooc.com/class/149.html
Stars: ✭ 39 (-69.29%)
Mutual labels:  concurrency, concurrent
easy-promise-queue
An easy JavaScript Promise queue which is automatically executed, concurrency controlled and suspendable.
Stars: ✭ 31 (-75.59%)
Mutual labels:  queue, concurrency
Hunch
Hunch provides functions like: All, First, Retry, Waterfall etc., that makes asynchronous flow control more intuitive.
Stars: ✭ 94 (-25.98%)
Mutual labels:  concurrency, concurrent
psched
Priority-based Task Scheduling for Modern C++
Stars: ✭ 59 (-53.54%)
Mutual labels:  queue, concurrency
Util
A collection of useful utility functions
Stars: ✭ 201 (+58.27%)
Mutual labels:  concurrency, concurrent
linked-blocking-multi-queue
A concurrent collection that extends the existing Java concurrent collection library, offering an optionally-bounded blocking "multi-queue" based on linked nodes.
Stars: ✭ 41 (-67.72%)
Mutual labels:  queue, concurrency
Spscqueue
A bounded single-producer single-consumer wait-free and lock-free queue written in C++11
Stars: ✭ 307 (+141.73%)
Mutual labels:  concurrency, queue
Freelancer
👔 An implementation of on-the-fly defined WebWorkers that are created inline using data URIs, rather than separate physical files — for the benefit of all humanity.
Stars: ✭ 57 (-55.12%)
Mutual labels:  concurrency, concurrent

go.dev reference godoc reference version Build Status Go Report Card codecov CodeFactor Mentioned in Awesome Go

goconcurrentqueue - Concurrent safe queues

The package goconcurrentqueue offers a public interface Queue with methods for a queue. It comes with multiple Queue's concurrent-safe implementations, meaning they could be used concurrently by multiple goroutines without adding race conditions.

Topics

Installation

Execute

go get github.com/enriquebris/goconcurrentqueue

This package is compatible with the following golang versions:

  • 1.7.x
  • 1.8.x
  • 1.9.x
  • 1.10.x
  • 1.11.x
  • 1.12.x
  • 1.13.x
  • 1.14.x

Documentation

Visit goconcurrentqueue at go.dev

Classes diagram

goconcurrentqueue class diagram

Queues

FIFO

FIFO: concurrent-safe auto expandable queue.

pros

  • It is possible to enqueue as many items as needed.
  • Extra methods to get and remove enqueued items:
    • Get: returns an element's value and keeps the element at the queue
    • Remove: removes an element (using a given position) from the queue

cons

  • It is slightly slower than FixedFIFO.

FixedFIFO

FixedFIFO: concurrent-safe fixed capacity queue.

pros

  • FixedFIFO is, at least, 2x faster than FIFO in concurrent scenarios (multiple GR accessing the queue simultaneously).

cons

  • It has a fixed capacity meaning that no more items than this capacity could coexist at the same time.

Benchmarks FixedFIFO vs FIFO

The numbers for the following charts were obtained by running the benchmarks in a 2012 MacBook Pro (2.3 GHz Intel Core i7 - 16 GB 1600 MHz DDR3) with golang v1.12

Enqueue

concurrent-safe FixedFIFO vs FIFO . operation: enqueue

Dequeue

concurrent-safe FixedFIFO vs FIFO . operation: dequeue

Get started

FIFO queue simple usage

Live code - playground

package main

import (
	"fmt"

	"github.com/enriquebris/goconcurrentqueue"
)

type AnyStruct struct {
	Field1 string
	Field2 int
}

func main() {
	queue := goconcurrentqueue.NewFIFO()

	queue.Enqueue("any string value")
	queue.Enqueue(5)
	queue.Enqueue(AnyStruct{Field1: "hello world", Field2: 15})

	// will output: 3
	fmt.Printf("queue's length: %v\n", queue.GetLen())

	item, err := queue.Dequeue()
	if err != nil {
		fmt.Println(err)
		return
	}

	// will output "any string value"
	fmt.Printf("dequeued item: %v\n", item)

	// will output: 2
	fmt.Printf("queue's length: %v\n", queue.GetLen())

}

Wait until an element gets enqueued

Live code - playground

package main

import (
	"fmt"
	"time"

	"github.com/enriquebris/goconcurrentqueue"
)

func main() {
	var (
		fifo = goconcurrentqueue.NewFIFO()
		done = make(chan struct{})
	)

	go func() {
		fmt.Println("1 - Waiting for next enqueued element")
		value, _ := fifo.DequeueOrWaitForNextElement()
		fmt.Printf("2 - Dequeued element: %v\n", value)

		done <- struct{}{}
	}()

	fmt.Println("3 - Go to sleep for 3 seconds")
	time.Sleep(3 * time.Second)

	fmt.Println("4 - Enqueue element")
	fifo.Enqueue(100)

	<-done
}

Wait until an element gets enqueued with timeout

Live code - playground

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/enriquebris/goconcurrentqueue"
)

func main() {
	var (
		fifo = goconcurrentqueue.NewFIFO()
		ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
	)
	defer cancel()

	fmt.Println("1 - Waiting for next enqueued element")
	_, err := fifo.DequeueOrWaitForNextElementContext(ctx)
    
	if err != nil {
		fmt.Printf("2 - Failed waiting for new element: %v\n", err)
		return
	}
}

Dependency Inversion Principle using concurrent-safe queues

High level modules should not depend on low level modules. Both should depend on abstractions. Robert C. Martin

Live code - playground

package main

import (
	"fmt"

	"github.com/enriquebris/goconcurrentqueue"
)

func main() {
	var (
		queue          goconcurrentqueue.Queue
		dummyCondition = true
	)

	// decides which Queue's implementation is the best option for this scenario
	if dummyCondition {
		queue = goconcurrentqueue.NewFIFO()
	} else {
		queue = goconcurrentqueue.NewFixedFIFO(10)
	}

	fmt.Printf("queue's length: %v\n", queue.GetLen())
	workWithQueue(queue)
	fmt.Printf("queue's length: %v\n", queue.GetLen())
}

// workWithQueue uses a goconcurrentqueue.Queue to perform the work
func workWithQueue(queue goconcurrentqueue.Queue) error {
	// do some work

	// enqueue an item
	if err := queue.Enqueue("test value"); err != nil {
		return err
	}

	return nil
}

History

v0.6.1

  • FixedFifo.Enqueue prevents to gets blocked trying to send the item over an invalid waitForNextElementChan channel

v0.6.0

  • Added DequeueOrWaitForNextElementContext()

v0.5.1

  • FIFO.DequeueOrWaitForNextElement() was modified to avoid deadlock when DequeueOrWaitForNextElement && Enqueue are invoked around the same time.
  • Added multiple goroutine unit testings for FIFO.DequeueOrWaitForNextElement()

v0.5.0

  • Added DequeueOrWaitForNextElement()

v0.4.0

  • Added QueueError (custom error)

v0.3.0

  • Added FixedFIFO queue's implementation (at least 2x faster than FIFO for multiple GRs)
  • Added benchmarks for both FIFO / FixedFIFO
  • Added GetCap() to Queue interface
  • Removed Get() and Remove() methods from Queue interface

v0.2.0

  • Added Lock/Unlock/IsLocked methods to control operations locking

v0.1.0

  • First In First Out (FIFO) queue added
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].