分享
go语言中的channel
jack_boy · · 2842 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
channel是go语言中的同步工具,有两种模式
执行结果:
hello word
此时,goroutines f() 先给a赋值,再向channel c中写入0;goroutines main() 阻塞在 <- c处,直到 f() 中 向管道中c中写数据语句: c <- 0 执行。
如果将两处管道操作调换顺序,执行结果为空。因为,main()将不阻塞直接print a,此时f()中a = "hello world"语句可能未执行,所以不一定能有hello world输出。
2. 再看看非缓冲方式的管道
执行结果:
hello world
此时,goroutines main()阻塞在写c <- 0语句处;goroutines f() 先给a赋值,再读管道c。当读c执行后,main()执行其后的语句,此时 a 是被赋值的。
注:其实读写管道都将可能被阻塞,直到有一方的读或写完成
此时,将两处管道操作调换顺序,同样也能够保证执行顺序
go语言的channel是一个重要的同步工具,正确的使用能大大减少同步带来的麻烦
- 缓冲 程序执行序列将阻塞在读channel的调用处 <- chan; 或当channel满时,阻塞在写channel调用处 chan <-。
- 非缓冲 程序执行序列将阻塞在读和写channel的调用处 chan <- 或 <- chan
引用
Sends to a buffered channel block only when the buffer is full. Receives block when the buffer is empty.
引用
sends and receives block until the other side is ready.
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输出。
2. 再看看非缓冲方式的管道
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是一个重要的同步工具,正确的使用能大大减少同步带来的麻烦
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信2842 次点击
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
- 缓冲 程序执行序列将阻塞在读channel的调用处 <- chan; 或当channel满时,阻塞在写channel调用处 chan <-。
- 非缓冲 程序执行序列将阻塞在读和写channel的调用处 chan <- 或 <- chan
引用
Sends to a buffered channel block only when the buffer is full. Receives block when the buffer is empty.
引用
sends and receives block until the other side is ready.
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输出。
2. 再看看非缓冲方式的管道
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是一个重要的同步工具,正确的使用能大大减少同步带来的麻烦