美文网首页
chan(rutime. hchan)结构

chan(rutime. hchan)结构

作者: EasyNetCN | 来源:发表于2021-02-23 15:24 被阅读0次

chan实际结构是runtime.hchan(https://github.com/golang/go/blob/master/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: chan 中已经接收但还没被取走的元素的个数。内建函数 len 可以返回这个字段的值。

dataqsiz:队列的大小。chan 使用一个循环队列来存放元素。

buf:存放元素的循环队列的 buffer

elemsize:buffer中元素大小

closed:是否close

elemtype:buffer中元素类型

sendx:处理发送数据的指针在 buf 中的位置。一旦接收了新的数据,指针就会加上 elemsize,移向下一个位置。buf 的总大小是 elemsize 的整数倍,而且 buf 是一个循环列表。

recvx:处理接收请求时的指针在 buf 中的位置。一旦取出数据,此指针会移动到下一个位置。

recvq:receiver等待队列。chan 是多生产者多消费者的模式,如果消费者因为没有数据可读而被阻塞了,就会被加入到 recvq 队列中。

sendq:sender等待队列。如果生产者因为 buf 满了而阻塞,会被加入到 sendq 队列中。

相关文章

  • chan(rutime. hchan)结构

    chan实际结构是runtime.hchan(https://github.com/golang/go/blob/...

  • go 的 channel 实现原理

    数据结构 src/runtime/chan.go:hchan 中定义了管道的数据结构: 从上面的数据结构可以看出来...

  • go channel

    [toc] Channel 编译器翻译 关键数据结构 hchan sudog hchan sudog 其中buf ...

  • chan深入理解之源码分析

    chan的理解 chan用于协程间通信,结构体如下,代码位置为go/src/runtime/chan.go 从结构...

  • Go

    数据结构 chan channel是golang进程内协程间通讯的管道 例子 输出: 性质 从chan读,如果ch...

  • 「Go」- golang源码分析 - channel的底层实现

    路径为:./src/runtime/chan.go 文件中,先看channel结构体: 以及waitq的结构体: ...

  • chan中的值(结构体)

  • channel 的实现

    1. 数据对应的数据结构 runtime.chan.go 2.channel 创建 通过make 创建channe...

  • Go 基础 4:channel拔高 和 select

    先说这个buffered chan就是在创建chan的时候加上个chan buffer的大小 基本的chan在se...

  • Chan

    梦已入睡 渡人自渡 渡一生莲花 …… 十里桃花 待嫁的年华 悠悠风来 埋在三生石下 …… 倘若我心中的如来 你眼中...

网友评论

      本文标题:chan(rutime. hchan)结构

      本文链接:https://www.haomeiwen.com/subject/oywrfltx.html