channel是golang中特有的一种数据结构,通常与goroutine一起使用,下面我们就介绍一下这种数据结构。
channel数据结构
channel
是Golang 中最重要的一个结构结构,源码里对应的结构体是hchan
,当我们创建一个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 }
字段说明
qcount
当前channel中的元素数量dataqsiz
环形队列的大小buf
指向dataqsize
的数组指针,只有缓冲chan有效closed
当前channel关闭状态elemsize
存储元素的大小elemtype
存储元素的数据类型sendx
发送操作处理到的索引位置,最大值为数组buf的最大下标值recvx
接收操作处理到的索引位置,最大值为数组buf的最大下标值recvq
接收队列,双向链表,阻塞元素sendq
发送列队,双向链表,阻塞元素lock
锁,,用来保护sudog里的所的字段

其中elemsize
和 elemtype
表示存储数据的大小和类型;sendx
和recvx
是指向底层数据的索引位置,表示当前处理的进度位置;recvq
和sendq
是一个由双向链表实现的队列,它存储的内容是由于队列dataqsize
过小,而阻塞的数据。