Skip to content

Instantly share code, notes, and snippets.

@wilkice
Last active January 7, 2022 05:22
Show Gist options
  • Save wilkice/4cc60bdc820168c440a876e75ccf626d to your computer and use it in GitHub Desktop.
Save wilkice/4cc60bdc820168c440a876e75ccf626d to your computer and use it in GitHub Desktop.
[go routine pool] #go
// simple go routine pool
package main
import (
"sync"
)
func main() {
var wg sync.WaitGroup
var routineNumber int
// var routineNumber int = 1000
// init go routine pool
pool := make(chan struct{}, routineNumber)
for {
pool <- struct{}{}
wg.Add(1)
go do(pool, &wg)
}
wg.Wait()
}
func do(pool chan int, wg *sync.WaitGroup) {
defer func() {
<- pool
wg.Done()
}()
// do something
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment