FAQ

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?
In general, synchronize only when you have to. Here the main
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.

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 7 of 7 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 29, '15 at 2:14p
activeJul 29, '15 at 9:12p
posts7
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase