worker-pool: init

This commit is contained in:
2024-02-15 18:39:41 +03:00
parent d1cb8beba3
commit 9ed5a2f85a
4 changed files with 126 additions and 0 deletions

21
worker-pool/main.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"fmt"
)
func WorkerPool[T any](limit int, worker func(int, T)) chan<- T {
tasks := make(chan T, limit)
for i := 1; i <= limit; i++ {
go func(i int) {
defer fmt.Printf("worker:%d done\n", i)
for t := range tasks {
worker(i, t)
}
}(i)
}
return tasks
}