How does make(chan bool) behave differently from make(chan bool, 1)?

后端 未结 3 1540
南方客
南方客 2020-12-31 08:52

My question arises from trying to read a channel, if I can, or write it, if I can, using a select statement.

I know that channels specified like m

3条回答
  •  礼貌的吻别
    2020-12-31 09:44

    Unbuffered channels (created without capacity) will block the sender until somebody can read from them, so to make it work as you expect, you should use two goroutines to avoid the deadlock in the same thread.

    For example, with this code: http://play.golang.org/p/KWJ1gbdSqf

    It also includes a mechanism for the main func to detect when both goroutines have finished.

    package main
    
    import "fmt"
    
    func send(out, finish chan bool) {
        for i := 0; i < 5; i++ {
            out <- true
            fmt.Println("Write")
        }
        finish <- true
        close(out)
    }
    
    func recv(in, finish chan bool) {
    
        for _ = range in {
            fmt.Println("Read")
        }
        finish <- true
    
    }
    
    func main() {
        chanFoo := make(chan bool)
        chanfinish := make(chan bool)
    
        go send(chanFoo, chanfinish)
        go recv(chanFoo, chanfinish)
    
        <-chanfinish
        <-chanfinish
    }
    

    It won't show the alternate Read and Write, because as soon as send writes to the channel, it is blocked, before it can print the "Write", so the execution moves to recv that receives the channel and prints "Read". It will try to read the channel again but it will be blocked and the execution moves to send. Now send can write the first "Write", send to the channel (without blocking because now there is a receiver ready) and write the second "Write". In any case, this is not deterministic and the scheduler may move the execution at any point to any other running goroutine (at least in the latest 1.2 release).

提交回复
热议问题