Go channel 实现原理分析

53 篇文章 0 订阅
23 篇文章 1 订阅

前言

channel一个类型管道,通过它可以在goroutine之间发送和接收消息。它是Golang在语言层面提供的goroutine间的通信方式。

众所周知,Go依赖于称为CSP(Communicating Sequential Processes)的并发模型,通过Channel实现这种同步模式。Go并发的核心哲学是不要通过共享内存进行通信; 相反,通过沟通分享记忆。

下面以简单的示例来演示Go如何通过channel来实现通信。

package main
import (
	"fmt"
	"time"
)
func goRoutineA(a <-chan int) {
	val := <-a
	fmt.Println("goRoutineA received the data", val)
}
func goRoutineB(b chan int) {
	val := <-b
	fmt.Println("goRoutineB  received the data", val)
}
func main() {
	ch := make(chan int, 3)
	go goRoutineA(ch)
	go goRoutineB(ch)
	ch <- 3
	time.Sleep(time.Second * 1)
}

结果为:

goRoutineA received the data 3

上面只是个简单的例子,只输出goRoutineA ,没有执行goRoutineB,说明channel仅允许被一个goroutine读写。

接下来我们通过源代码分析程序执行过程,在讲之前,如果不了解go 并发和调度相关知识。请阅读这篇文章

https://github.com/guyan0319/golang_development_notes/blob/master/zh/9.5.md

说道channel这里不得不提通道的结构hchan。

hchan

源代码在src/runtime/chan.go

type hchan struct {
   qcount   uint           // total data in the queue
   dataqsiz uint           // size of the circular queue
   buf      unsafe.Pointer // points to an array of dataqsiz elements
   elemsize uint16
   closed   uint32
   elemtype *_type // element type
   sendx    uint   // send index
   recvx    uint   // receive index
   recvq    waitq  // list of recv waiters
   sendq    waitq  // list of send waiters

   // lock protects all fields in hchan, as well as several
   // fields in sudogs blocked on this channel.
   //
   // Do not change another G's status while holding this lock
   // (in particular, do not ready a G), as this can deadlock
   // with stack shrinking.
   lock mutex
}
type waitq struct {
	first *sudog
	last  *sudog
}

说明:

qcount uint // 当前队列中剩余元素个数
dataqsiz uint // 环形队列长度,即缓冲区的大小,即make(chan T,N),N.
buf unsafe.Pointer // 环形队列指针
elemsize uint16 // 每个元素的大小
closed uint32 // 表示当前通道是否处于关闭状态。创建通道后,该字段设置为0,即通道打开; 通过调用close将其设置为1,通道关闭。
elemtype *_type // 元素类型,用于数据传递过程中的赋值;
sendx uint和recvx uint是环形缓冲区的状态字段,它指示缓冲区的当前索引 - 支持数组,它可以从中发送数据和接收数据。
recvq waitq // 等待读消息的goroutine队列
sendq waitq // 等待写消息的goroutine队列
lock mutex // 互斥锁,为每个读写操作锁定通道,因为发送和接收必须是互斥操作。

这里sudog代表goroutine。

创建channel 有两种,一种是带缓冲的channel,一种是不带缓冲的channel

// 带缓冲
ch := make(chan Task, 3)
// 不带缓冲
ch := make(chan int)

这里我们先讨论带缓冲

ch := make(chan int, 3)

创建通道后的缓冲通道结构

hchan struct {
	qcount uint : 0 
	dataqsiz uint : 3 
	buf unsafe.Pointer : 0xc00007e0e0 
	elemsize uint16 : 8 
	closed uint32 : 0 
	elemtype *runtime._type : &{
		size:8 
		ptrdata:0 
		hash:4149441018 
		tflag:7 
		align:8 
		fieldalign:8 
		kind:130 
		alg:0x55cdf0 
		gcdata:0x4d61b4 
		str:1055 
		ptrToThis:45152
		}
	sendx uint : 0 
	recvx uint : 0 
	recvq runtime.waitq : 
		{first:<nil> last:<nil>}
	sendq runtime.waitq : 
		{first:<nil> last:<nil>}
	lock runtime.mutex : 
		{key:0}
}

源代码

func makechan(t *chantype, size int) *hchan {

   elem := t.elem
   ...
}

如果我们创建一个带buffer的channel,底层的数据模型如下图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DwxuZIxP-1584289657057)(https://github.com/guyan0319/golang_development_notes/blob/master/images/9.9.3.png?raw=true)]

向channel写入数据

ch <- 3

底层hchan数据流程如图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ECuSZS0o-1584289657059)(https://github.com/guyan0319/golang_development_notes/blob/master/images/9.9.5.png?raw=true)]

发送操作概要

1、锁定整个通道结构。

2、确定写入。尝试recvq从等待队列中等待goroutine,然后将元素直接写入goroutine。

3、如果recvq为Empty,则确定缓冲区是否可用。如果可用,从当前goroutine复制数据到缓冲区。

4、如果缓冲区已满,则要写入的元素将保存在当前正在执行的goroutine的结构中,并且当前goroutine将在sendq中排队并从运行时挂起。

5、写入完成释放锁。

这里我们要注意几个属性buf、sendx、lock的变化。

流程图

从channel读取操作

几乎和写入操作相同

代码

func goRoutineA(a <-chan int) {
   val := <-a
   fmt.Println("goRoutineA received the data", val)
}

底层hchan数据流程如图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Z3t2qUs2-1584289657065)(https://github.com/guyan0319/golang_development_notes/blob/master/images/9.9.8.png?raw=true)]

这里我们要注意几个属性buf、sendx、recvx、lock的变化。

读取操作概要

1、先获取channel全局锁

2、尝试sendq从等待队列中获取等待的goroutine,

3、 如有等待的goroutine,没有缓冲区,取出goroutine并读取数据,然后唤醒这个goroutine,结束读取释放锁。

4、如有等待的goroutine,且有缓冲区(此时缓冲区已满),从缓冲区队首取出数据,再从sendq取出一个goroutine,将goroutine中的数据存入buf队尾,结束读取释放锁。

5、如没有等待的goroutine,且缓冲区有数据,直接读取缓冲区数据,结束读取释放锁。

6、如没有等待的goroutine,且没有缓冲区或缓冲区为空,将当前的goroutine加入recvq排队,进入睡眠,等待被写goroutine唤醒。结束读取释放锁。

流程图

recvq和sendq 结构

recvq和sendq基本上是链表,看起来基本如下

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yK9oK7Ng-1584289657067)(https://github.com/guyan0319/golang_development_notes/blob/master/images/9.9.11.jpeg?raw=true)]

select

select就是用来监听和channel有关的IO操作,当 IO 操作发生时,触发相应的动作。

一个简单的示例如下

package main

import (
   "fmt"
   "time"
)

func goRoutineD(ch chan int, i int) {
   time.Sleep(time.Second * 3)
   ch <- i
}
func goRoutineE(chs chan string, i string) {
   time.Sleep(time.Second * 3)
   chs <- i

}

func main() {
   ch := make(chan int, 5)
   chs := make(chan string, 5)

   go goRoutineD(ch, 5)
   go goRoutineE(chs, "ok")

	select {
	case msg := <-ch:
		fmt.Println(" received the data ", msg)
	case msgs := <-chs:
		fmt.Println(" received the data ", msgs)
	default:
		fmt.Println("no data received ")
		time.Sleep(time.Second * 1)
	}


}

运行程序,因为当前时间没有到3s,所以select 选择defult

no data received

修改程序,我们注释掉default,并多执行几次结果为

received the data 5

received the data ok

received the data ok

received the data ok

select语句会阻塞,直到监测到一个可以执行的IO操作为止,而这里goRoutineD和goRoutineE睡眠时间是相同的,都是3s,从输出可看出,从channel中读出数据的顺序是随机的。

再修改代码,goRoutineD睡眠时间改成4s

func goRoutineD(ch chan int, i int) {
   time.Sleep(time.Second * 4)
   ch <- i
}

此时会先执行goRoutineE,select 选择case msgs := <-chs。

range

可以持续从channel读取数据,一直到channel被关闭,当channel中没有数据时会阻塞当前goroutine,与读channel时阻塞处理机制一样。

package main

import (
   "fmt"
   "time"
)

func goRoutineD(ch chan int, i int) {
   for   i := 1; i <= 5; i++{
      ch <- i
   }

}
func chanRange(chanName chan int) {
   for e := range chanName {
      fmt.Printf("Get element from chan: %d\n", e)
      if len(chanName) <= 0 { // 如果现有数据量为0,跳出循环
			break
	  }
   }
}
func main() {
   ch := make(chan int, 5)
   go goRoutineD(ch, 5)
   chanRange(ch)

}

结果:
Get element from chan: 1
Get element from chan: 2
Get element from chan: 3
Get element from chan: 4
Get element from chan: 5

死锁(deadlock)

指两个或两个以上的协程的执行过程中,由于竞争资源或由于彼此通信而造成的一种阻塞的现象。

在非缓冲信道若发生只流入不流出,或只流出不流入,就会发生死锁。

下面是一些死锁的例子

1、

package main

func main() {
   ch := make(chan int)
   ch <- 3
}

上面情况,向非缓冲通道写数据会发生阻塞,导致死锁。解决办法创建缓冲区 ch := make(chan int,3)

2、

package main

import (
   "fmt"
)

func main() {
   ch := make(chan int)
   fmt.Println(<-ch)
}

向非缓冲通道读取数据会发生阻塞,导致死锁。 解决办法开启缓冲区,先向channel写入数据。

3、

package main

func main() {
   ch := make(chan int, 3)
   ch <- 3
   ch <- 4
   ch <- 5
   ch <- 6
}

写入数据超过缓冲区数量也会发生死锁。解决办法将写入数据取走。

死锁的情况有很多这里不再赘述。
还有一种情况,向关闭的channel写入数据,不会产生死锁,产生panic。

package main

func main() {
	ch := make(chan int, 3)
	ch <- 1
	close(ch)
	ch <- 2

}

解决办法别向关闭的channel写入数据。

参考:

https://codeburst.io/diving-deep-into-the-golang-channels-549fd4ed21a8

https://speakerdeck.com/kavya719/understanding-channels?slide=14

https://my.oschina.net/renhc/blog/2246871

links

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Go 中的 Channel 是并发编程中非常重要的一种数据结构,它提供了一种通信机制,使得不同的 Goroutine 可以在不使用显式锁的情况下进行数据交换。那么它的底层实现原理是什么呢? Go 中的 Channel 的底层实现是基于 Goroutine 和同步原语的。每个 Channel 都有一个相关联的等待队列,它用来存储等待在 Channel 上的 Goroutine。当一个 Goroutine 向 Channel 写入数据时,如果当前 Channel 中没有空闲的缓存区,那么这个 Goroutine 就会被阻塞,并被加入到等待队列中。如果有其他 Goroutine 从 Channel 中读取了数据,那么这个等待队列中的 Goroutine 就会被唤醒,继续执行。 类似地,当一个 Goroutine 从 Channel 中读取数据时,如果当前 Channel 中没有数据可读,那么这个 Goroutine 就会被阻塞,并被加入到等待队列中。如果有其他 Goroutine 向 Channel 中写入了数据,那么这个等待队列中的 Goroutine 就会被唤醒,继续执行。 需要注意的是,Go 中的 Channel 是类型安全的。也就是说,一个只能接收字符串的 Channel 是不能接收整数的。这是因为 Channel 内部维护了一个元素类型的信息,它会在运行时进行检查,确保只有符合类型要求的数据才能被写入或读取。 总之,Go 中的 Channel 的底层实现是基于 Goroutine 和同步原语的,并且是类型安全的。它提供了一种非常方便的并发编程机制,使得不同的 Goroutine 可以在不使用显式锁的情况下进行数据交换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值