go语言中的channel

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] 先来看看缓冲方式
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是一个重要的同步工具,正确的使用能大大减少同步带来的麻烦
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值