What channel operations are directly used in the synchronization process?
package main
func main() {
tasks := make(chan int) // (1)
sync := make(chan bool, 1) // (1)
workers := 2
done := make(chan bool, workers) // (1)
for range make([]struct{}, workers) {
go doWork(tasks, sync, done)
}
close(tasks) // (4)
for workers > 0 {
<-done // (3)
workers--
}
}
func doWork(tasks <-chan int, sync chan bool, done chan<- bool) {
for range tasks {
// do work
sync <- true // (2)
// access to shared source
<-sync // (3)
}
done <- true // (2)
}