Last active
January 7, 2022 05:22
-
-
Save wilkice/4cc60bdc820168c440a876e75ccf626d to your computer and use it in GitHub Desktop.
[go routine pool] #go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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