Skip to content

Instantly share code, notes, and snippets.

@repejota
Created March 10, 2017 12:55
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save repejota/ed9070d57c23102d50c94e1a126b2f5b to your computer and use it in GitHub Desktop.
Save repejota/ed9070d57c23102d50c94e1a126b2f5b to your computer and use it in GitHub Desktop.
Evar (
concurrent = 5
semaphoreChan = make(chan struct{}, concurrent)
)
func doWork(item int) {
semaphoreChan <- struct{}{} // block while full
go func() {
defer func() {
<-semaphoreChan // read to release a slot
}()
log.Println(item)
time.Sleep(1 * time.Second)
}()
}
func main() {
for i := 0; i < 10000; i++ {
doWork(i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment