Skip to content

Instantly share code, notes, and snippets.

@yanjinbin
Created June 9, 2019 18:19
Show Gist options
  • Save yanjinbin/892fee22d119284dbea6c3614c0976c4 to your computer and use it in GitHub Desktop.
Save yanjinbin/892fee22d119284dbea6c3614c0976c4 to your computer and use it in GitHub Desktop.
为什么Chan2方法可以跑通,Chan3一直无限循环呢
func Chan3() {
	<-c
	c <- 0
	fmt.Println("done")
}

var c = make(chan int)
var a string

func f() {
	a = "hello, world" //1
	<-c                //2
}

func Chan2() {
	go f()              //3
	c <- 0              //4
	fmt.Print("打印:", a) //5
}
@yanjinbin
Copy link
Author

为什么Chan2方法可以打印出hello world,Chan3一直无限循环呢?

@abserari
Copy link

因为 3 里面的 channel 通道阻塞了。

@yanjinbin
Copy link
Author

因为 3 里面的 channel 通道阻塞了。

https://stackoverflow.com/questions/18660533/why-using-unbuffered-channel-in-the-same-goroutine-gives-a-deadlock
这篇文章讲的不错 关于unbuffer channel 的 同步

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment