This is an concurrent-queue and concurrent-stack lib for the go.

Go Reference
license
GoCover
size


Getting Started

Pull in the dependency

go get github.com/boobusy/vector

Add the import to your project

import (
    "github.com/boobusy/vector"
)

Simple

func main() {
	for i := 0; i < 100; i++ {
		vector.Push(i)
	}

	fmt.Println(vector.IsEmpty(),vector.Len(),vector.Cap())
	fmt.Println(vector.Items())

	for !vector.IsEmpty() {
		fmt.Println(vector.PopBack()) // or fmt.Println(vector.PopFront())
	}

}

thread-safe

func main() {

    var queue *vector.Type = vector.New(10000)
	//queue := vector.New(10000)

	// auto clean
	queue.UsePurge(1<<20, 100 * time.Millisecond)

	w := sync.WaitGroup{}
	for i := 0; i < 100; i++ {
		w.Add(1)
		go func() {
			defer w.Done()
			for k := 0; k < 1000; k++ {
				task := &Task{
					Id: k,
				}
				queue.Push(task)
			}
		}()
	}

	w.Wait()
	fmt.Println(queue.IsEmpty(), queue.Len(), queue.Cap())

	var val vector.Val
	for !queue.IsEmpty() {
        val = queue.PopBack()
		fmt.Println(val.(*Task).Id) // or fmt.Println(vector.PopFront())
	}

}

Examples

author

问题或者建议联系作者:十二楼五城

boobusy

GitHub

View Github