worker

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package worker provides a simple goroutine pool for background task execution.

Create a pool with a fixed number of workers, submit tasks, and wait for completion:

pool := worker.NewPool(4)
for _, item := range items {
    item := item
    pool.Submit(func() { process(item) })
}
pool.WaitAll()

Pool.Submit queues a Task for execution by one of the pool's workers. Pool.WaitAll blocks until every submitted task has finished.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

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

Pool is a pool of goroutines which can run tasks concurrently

Example
package main

import (
	"fmt"
	"sync/atomic"

	"github.com/luanguimaraesla/garlic/worker"
)

func main() {
	pool := worker.NewPool(2)

	var count atomic.Int32
	for i := 0; i < 5; i++ {
		pool.Submit(func() {
			count.Add(1)
		})
	}
	pool.WaitAll()

	fmt.Println(count.Load())
}
Output:
5

func NewPool

func NewPool(size int) *Pool

NewPool creates a worker pool with the given size

func (*Pool) Submit

func (p *Pool) Submit(task Task)

Submit submits a new task for the workers to run

func (*Pool) WaitAll

func (p *Pool) WaitAll()

WaitAll waits until every task submited has finished

type Task

type Task func()

Jump to

Keyboard shortcuts

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