go channel

2020-09-15  本文已影响0人  心似南风

go version:1.12.5
文件 :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 mutex
}

看图中结构体数据变化


image.png image.png

执行逻辑


参考《GO专家编程》

上一篇 下一篇

猜你喜欢

热点阅读