On Wed, 29 Jul 2015 01:39:50 PDT Yesudeep Mangalapilly wrote:
--- code ---
package main
import "fmt"
import "time"
func worker(done chan bool) {
time.Sleep(time.Second)
done <- true
}
func main() {
done := make(chan bool, 1) // What is the significance of using 1 as
capacity here?
go worker(done)
<-done
}
--- end code ---
Why use 1 to create a buffered channel? Can we not signal "done" using a
regular non-buffered channel?
--- code ---
package main
import "fmt"
import "time"
func worker(done chan bool) {
time.Sleep(time.Second)
done <- true
}
func main() {
done := make(chan bool, 1) // What is the significance of using 1 as
capacity here?
go worker(done)
<-done
}
--- end code ---
Why use 1 to create a buffered channel? Can we not signal "done" using a
regular non-buffered channel?
thread wants to know when the worker thread terminates but the
worker thread doesn't care when the main thread gets around to
reading from "done". Using a 1 deep buffer channel exactly
captures this usage pattern. An unbuffered channel would make
the worker thread "rendezvous" with the main thread, which is
unnecessary.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.