geoah / go-queue

Lightweight, thread-safe, blocking FIFO queue based on auto-resizing circular buffer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Queue

Software License

Lightweight, tested, performant, thread-safe, blocking FIFO queue based on auto-resizing circular buffer.

This fork has some differences from the original sheerun/queue:

  • Switches to use generics for items
  • Removes the Remove() method
  • Adds error returns to all methods
  • Adds Close() method

Usage

package main

import (
  "fmt"
  "sync"
  "time"

  "github.com/geoah/go-queue/v3"
)

func main() {
  q := queue.New[int]()
  var wg sync.WaitGroup
  wg.Add(2)

  // Worker 1
  go func() {
    for i := 0; i < 500; i++ {
      item, err := q.Pop()
      fmt.Printf("%v, err=%s\n", item, err)
      time.Sleep(10 * time.Millisecond)
    }
    wg.Done()
  }()

  // Worker 2
  go func() {
    for i := 0; i < 500; i++ {
      item, err := q.Pop()
      fmt.Printf("%v, err=%s\n", item, err)
      time.Sleep(10 * time.Millisecond)
    }
    wg.Done()
  }()

  for i := 0; i < 1000; i++ {
    q.Append(i)
  }

  wg.Wait()
}

License

MIT

About

Lightweight, thread-safe, blocking FIFO queue based on auto-resizing circular buffer


Languages

Language:Go 100.0%