Created
March 10, 2017 12:55
-
-
Save repejota/ed9070d57c23102d50c94e1a126b2f5b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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