简体   繁体   English

为什么golang中的频道需要执行常规程序?

[英]Why does a channel in golang require a go-routine?

I am coming upto speed on channels in golang. 我正赶上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. 我从使用go例程的示例中了解了它的用法。 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. OK,在上面的示例中,当遇到c <- 17时,发送方(主例程) 准备好发送。 So shouldn't that execute. 所以不应该执行。 Subsequently the Println should be able to drain the channel. 随后, Println应该能够排空通道。

I realize everything works fine if c <- 17 is replaced by 我意识到如果将c <- 17替换为c <- 17一切正常

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

Just trying to understand why that's necessary. 只是想了解为什么这是必要的。

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. 确实:由于没有go例程正在等待接收,因此发送被阻塞,程序陷入僵局。 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语句中使用send运算符:

select {
case c <- 17:
default:
}

The Go Programming Language Specification Go编程语言规范

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创建新的初始化通道值,该函数将通道类型和可选容量作为参数:

 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? 您问:为什么golang中的频道需要执行常规程序?

A Go channel does not require a goroutine. Go频道不需要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. @Tim Cooper提出的解决方案具有相同的错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM