channel是go语言中的同步工具,有两种模式
[list]
[*] 缓冲
程序执行序列将阻塞在读channel的调用处 <- chan; 或当channel满时,阻塞在写channel调用处 chan <-。
[quote][color=red][b]Sends to a buffered channel block only when the buffer is full. Receives block when the buffer is empty.[/b][/color][/quote]
[*] 非缓冲
程序执行序列将阻塞在读和写channel的调用处 chan <- 或 <- chan
[quote][color=red][b]sends and receives block until the other side is ready.[/b][/color][/quote]
[/list][b]1.[/b] 先来看看缓冲方式
执行结果:
hello word
此时,goroutines f() 先给a赋值,再向channel c中写入0;goroutines main() 阻塞在 <- c处,直到 f() 中 向管道中c中写数据语句: c <- 0 执行。
如果将两处管道操作调换顺序,执行结果为空。因为,main()将不阻塞直接print a,此时f()中a = "hello world"语句可能未执行,所以不一定能有hello world输出。
[b]2. [/b]再看看非缓冲方式的管道
执行结果:
hello world
此时,goroutines main()阻塞在写c <- 0语句处;goroutines f() 先给a赋值,再读管道c。当读c执行后,main()执行其后的语句,此时 a 是被赋值的。
注:其实读写管道都将可能被阻塞,直到有一方的读或写完成
此时,将两处管道操作调换顺序,同样也能够保证执行顺序
go语言的channel是一个重要的同步工具,正确的使用能大大减少同步带来的麻烦
[list]
[*] 缓冲
程序执行序列将阻塞在读channel的调用处 <- chan; 或当channel满时,阻塞在写channel调用处 chan <-。
[quote][color=red][b]Sends to a buffered channel block only when the buffer is full. Receives block when the buffer is empty.[/b][/color][/quote]
[*] 非缓冲
程序执行序列将阻塞在读和写channel的调用处 chan <- 或 <- chan
[quote][color=red][b]sends and receives block until the other side is ready.[/b][/color][/quote]
[/list][b]1.[/b] 先来看看缓冲方式
package main
var a string
var c = make(chan int, 10)
func f() {
a = "hello world"
c <- 0
//<- c
}
func main() {
go f()
<- c
//c <- 0
print(a)
}
执行结果:
hello word
此时,goroutines f() 先给a赋值,再向channel c中写入0;goroutines main() 阻塞在 <- c处,直到 f() 中 向管道中c中写数据语句: c <- 0 执行。
如果将两处管道操作调换顺序,执行结果为空。因为,main()将不阻塞直接print a,此时f()中a = "hello world"语句可能未执行,所以不一定能有hello world输出。
[b]2. [/b]再看看非缓冲方式的管道
package main
var a string
var c = make(chan int)
func f() {
a = "hello world"
//c <- 0
<- c
}
func main() {
go f()
//<- c
c <- 0
print(a)
}
执行结果:
hello world
此时,goroutines main()阻塞在写c <- 0语句处;goroutines f() 先给a赋值,再读管道c。当读c执行后,main()执行其后的语句,此时 a 是被赋值的。
注:其实读写管道都将可能被阻塞,直到有一方的读或写完成
此时,将两处管道操作调换顺序,同样也能够保证执行顺序
go语言的channel是一个重要的同步工具,正确的使用能大大减少同步带来的麻烦