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
Click to show internal directories.
Click to hide internal directories.