11
理解golang的channel
source link: https://doumao.cc/index.php/%E7%BC%96%E7%A8%8B/%E7%90%86%E8%A7%A3golang%E7%9A%84channel.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
channel是golang里很重要的一种数据结构
channel的特性
0QRSAO.jpgchannel内部实现
buffered channel
在底层使用一个struct
来实现,使用一个环形缓冲区circular queue
来保存元素。
向channel发送值
channel的buffer已满
0QRdC4.jpg从channel接收值
ps:
使用make来创建channel时,go在堆上分配一个
hchan struct
,然后初始化它,最后返回的是一个指向hchan struct
的指针。channel发送和接收值的过程
buffered channel
之所以能够goruntine安全,因为每次send
和receive
时,都会对结构体内的mutex
上锁。
下面是send
的过程。
0QWiIU.jpg
0QWPaT.jpg
下面是receive
的过程。
0QW3Je.jpg
0QW1iD.jpg
channel如何暂停(Pause)和恢复(Resume) goroutine
0lCoV0.jpg先了解下Go的调度器和GMP
0lPJLn.jpggoroutine可以看做是用户态线程
,goroutine由go运行时创建和管理,被调度器调度到系统线程。
暂停(Pause)goroutine
0li3tK.jpg可以看到,当channel满时,被阻塞的只是goroutine,系统线程仍处在运行状态。
恢复(Resume)goroutine
01cS4e.jpg01cC3d.jpg当从一个满的channel接收值时
01cwvR.jpg01cuCQ.jpg01cZE8.jpg01ceUS.jpg01cm4g.jpgGopherCon 2017: Kavya Joshi - Understanding Channels
Kavya Joshi - Understanding Channels.pdf
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK