Created
January 29, 2019 08:51
-
-
Save xsaamiir/62d4fec3de1474ef9d8b5b8d55943bcc to your computer and use it in GitHub Desktop.
Golang goroutines semaphores
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
package main | |
import "fmt" | |
// sem is a channel that will allow up to 10 concurrent operations. | |
var sem = make(chan int, 10) | |
func main() { | |
for { | |
sem <- 1 // will block if there is MAX ints in sem | |
go func() { | |
fmt.Println("hello again, world") | |
<-sem // removes an int from sem, allowing another to proceed | |
}() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment