channel底层实现

go:1.16

type hchan struct {
	qcount   uint           // chan里的元素数量
	dataqsiz uint           // chan底层循环数组长度
	buf      unsafe.Pointer // 指向底层循环数组指针,只针对有缓冲的channel
	elemsize uint16         // chan中元素大小
	closed   uint32         // chan是否被关闭
	elemtype *_type // chan中元素类型
	sendx    uint   // 已发送元素在循环列表中的索引
	recvx    uint   // 已接收元素在循环列表中的索引
	recvq    waitq  // 等待接收的goroutine队列
	sendq    waitq  // 等待发送的goroutine队列

	// 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 // 保护hchan中的所有字段
}

  

channel底层实现

上一篇:执行上下文和作用域链


下一篇:SSO单点登录-CAS验证