在 Go 语言中,使用两个 fmt.Println() 打印函数会导致死锁吗?这是一个常见的问题,让我们来解答一下。首先,要了解死锁的概念。死锁是指两个或多个进程互相等待对方完成的情况,导致程序无法继续执行。在 Go 语言中,如果在两个例程中同时使用 fmt.Println() 打印函数,由于标准输出是线程安全的,所以不会出现死锁的情况。因此,你可以放心地在 go 例程中使用多个 fmt.Println() 函数,而无需担心死锁问题。
问题内容
我正在尝试学习 go,并且正在操场上进行实验。我有一个非常简单的代码。我试图在 go 例程中一起使用结构和切片。我不确定这是否会是我在生产中使用的东西,但它似乎有点不对劲,所以这里:
func main() {
routinemsg := make(chan []Person)
routinemsg2 := make(chan []Person)
// create the person records
p1 := newPerson("john doe", 25)
p2 := newPerson("dohn joe", 52)
p3 := newPerson("bohn joo", 30)
// send a slice of Person to the first routine
go func() { routinemsg <- []Person{p1, p2} }()
// retrieve the slice from the first routine[in the append]
// append p3 to the slice retrieved from the first routine
// send the new slice to the second routine
go func() { routinemsg2 <- append(<-routinemsg, p3) }()
// I am able to see the first Println but when I insert the second one I get a deadlock error
// also, same error if I use one Println with 2 arguments.
fmt.Println(<-routinemsg)
fmt.Println(<-routinemsg2)
}
我听说过等待组,但还不了解它们!所以,对我好一点:d,感谢您抽出时间
解决方法
routinemsg
上只有一个发送操作,但您有 2 个接收操作:一个在启动的 goroutine 中,另一个在 main
goroutine 中。发送的值只能由一个接收者接收一次。
如果启动的goroutine先从routinemsg
接收,那么就会出现死锁:main
中的接收将永远阻塞。
如果 main
goroutine 首先接收,那么启动的 goroutine 将永远阻塞(尝试从中接收),因此它永远无法在 routinemsg2
上发送任何内容,因此 main
中从 routinemsg2
接收也将永远阻塞:再次死锁。
删除 main()
中的 fmt.println(<-routinemsg)
行,然后从 routinemsg2
的最终接收可以(最终)继续并打印包含 p1
、p2
和 phpcnc 的切片phpcnp3:
[{john doe 25} {dohn joe 52} {bohn joo 30}]
在 go playground 上尝试一下。