Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a channel in golang require a go-routine?

Tags:

go

I am coming upto speed on channels in golang. Per its documentation,

Channels are a typed conduit through which you can send and receive values with the channel operator, <-.

I get that. I understand how it is used from examples that utilize go routines. I tried an extremely trivial example. It results in a deadlocked program. Ignoring the pointlessness of this program can you please tell me why this is deadlocked?

package main
import  "fmt"
func main() {
    c := make(chan int)
    c <- 17
    fmt.Println(<- c)
}

The referenced documentation adds that

By default, sends and receives block until the other side is ready.

OK, in the above example, the sender (the main routine) is ready to send when c <- 17 is encountered. So shouldn't that execute. Subsequently the Println should be able to drain the channel.

I realize everything works fine if c <- 17 is replaced by

go func() { c <- 17 } ()

Just trying to understand why that's necessary.

like image 841
Sri Sankaran Avatar asked May 27 '17 11:05

Sri Sankaran


2 Answers

The Go Programming Language Specification

Channel types

A channel provides a mechanism for concurrently executing functions to communicate by sending and receiving values of a specified element type.

A new, initialized channel value can be made using the built-in function make, which takes the channel type and an optional capacity as arguments:

make(chan int, 100)

The capacity, in number of elements, sets the size of the buffer in the channel. If the capacity is zero or absent, the channel is unbuffered and communication succeeds only when both a sender and receiver are ready. Otherwise, the channel is buffered and communication succeeds without blocking if the buffer is not full (sends) or not empty (receives). A nil channel is never ready for communication.


You ask: Why does a channel in golang require a go-routine?

A Go channel does not require a goroutine. A successful send merely requires a ready receiver or a buffer that is not full. For example, using a buffered channel,

package main

import "fmt"

func main() {
    c := make(chan int, 1)
    c <- 17
    fmt.Println(<-c)
}

Output:

17

Your example fails because it tries to send on an unbuffered channel that does not have a ready receiver.

package main

import "fmt"

func main() {
    c := make(chan int)
    c <- 17
    fmt.Println(<-c)
}

Output:

fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.main()
    /home/peter/gopath/src/sri.go:7 +0x59

@Tim Cooper's proposed solution has the same error.

package main

import "fmt"

func main() {
    c := make(chan int)
    select {
    case c <- 17:
    default:
    }
    fmt.Println(<-c)
}

Output:

fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
    /home/peter/gopath/src/tim.go:11 +0x8a
like image 152
peterSO Avatar answered Sep 30 '22 16:09

peterSO


By default, sends and receives block until the other side is ready.

Exactly: since no go routine is waiting to receive, the send is blocked, and your program deadlocks. The send operation does not get skipped over because no one is waiting to receive.

If you want to do a non-blocking send, you would use the send operator in a select statement with a default case:

select {
case c <- 17:
default:
}
like image 23
Tim Cooper Avatar answered Sep 30 '22 18:09

Tim Cooper