Golang-发生死锁的原因

91 阅读1分钟

发生死锁的原因

  1. 主要是主协程因为channel而被阻塞,就会报dead lock。

往没有make的channel里读写数据,都会报错

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive (nil chan)]:

主协程阻塞而报deadlock的例子:

package main

import (
	"fmt"
)

func main() {
	deadlockTest()
	//time.Sleep(time.Second * 60)
}

func deadlockTest() {
	fmt.Println("[deadlockTest] start")
	ch := make(chan int)
	results := make(chan int)

	for i := 0; i < 2; i++ {
		go func() {
			// 把从channel里取得的数据,再传回去
			x := <-ch
			results <- x

		}()
	}

	// 向输入数据里传两个数据
	ch <- 1
	ch <- 2

	for re := range results {
		fmt.Printf("re:%v\n", re)
	}
	fmt.Println("[deadlockTest] end")
}

输出:

dev@dev-VirtualBox test $ go run test_channel.go 
[deadlockTest] start
re:1
re:2
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.deadlockTest()
        /home/dev/test/test_channel.go:30 +0x1fc
main.main()
        /home/dev/test/test_channel.go:8 +0x20
exit status 2

主协程使用range读results信道,然后因为没有把results关闭掉,所以阻塞了。现在直接把deadlockTest方法go出去,就不会阻塞主协程了,因而就不会报deadlock。

package main

import (
	"fmt"
	"time"
)

func main() {
	go deadlockTest()
	time.Sleep(time.Second * 60)
}

func deadlockTest() {
	fmt.Println("[deadlockTest] start")
	ch := make(chan int)
	results := make(chan int)

	for i := 0; i < 2; i++ {
		go func() {
			// 把从channel里取得的数据,再传回去
			x := <-ch
			results <- x

		}()
	}

	// 向输入数据里传两个数据
	ch <- 1
	ch <- 2

	for re := range results {
		fmt.Printf("re:%v\n", re)
	}
	fmt.Println("[deadlockTest] end")
}

输出:

dev@dev-VirtualBox test $ go run test_channel.go 
[deadlockTest] start
re:1
re:2
dev@dev-VirtualBox test $