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?
A goroutine is a lightweight execution thread in the Go programming language and a function that executes concurrently with the rest of the program. Goroutines are incredibly cheap when compared to traditional threads as the overhead of creating a goroutine is very low.
Golang provides goroutines to support concurrency in Go. A goroutine is a function that executes simultaneously with other goroutines in a program and are lightweight threads managed by Go.
A goroutine is a function that runs independently of the function that started it. Sometimes Go developers explain a goroutine as a function that runs as if it were on its own thread. Channel is a pipeline for sending and receiving data. Think of it as a socket that runs inside your program.
In Golang, or Go, channels are a means through which different goroutines communicate. Think of them as pipes through which you can connect with different concurrent goroutines. The communication is bidirectional by default, meaning that you can send and receive values from the same channel.
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:
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With