Are goroutines garbage collected together with their channels?

巧了我就是萌 提交于 2020-01-14 08:59:10

问题


Imagine the following code:

func waitForOneOfTwoProcesses() {

    c := make(chan bool)
    go func() {
        time.Sleep(1 * time.Second)
        c<-true
    }()
    go func() {
        time.Sleep(2 * time.Second)
        c<-true
    }()
    <-c

}

Does this leak a channel and a goroutine or does Go recognize that c is gone and the goroutine can exit?

Would the answer be any different if the channel had a buffer size of 2?


回答1:


If the channel is unbuffered, then one of the anonymous functions will not return. The program leaks a goroutine and channel.

If the channel has a buffer size greater than or equal to one, then both of the anonymous functions will return. The resources used by the goroutines and channel will be reclaimed.

A buffer size of one is sufficient to prevent the leak. The function waitForOneOfTwoProcesses receives one of the values sent to c. The second value sent to c is buffered in the channel (which is collected by the GC).

Another way to ensure that the goroutines return is to use non-blocking send. Replace the c <- true lines with:

 select {
 case c <- true:
 default:
 }


来源:https://stackoverflow.com/questions/38090545/are-goroutines-garbage-collected-together-with-their-channels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!