当前位置:网站首页>Go Chan's underlying principles
Go Chan's underlying principles
2022-07-02 04:50:00 【Chirp cat】
Go chan The underlying principle of
What is? channel?
chan yes Go A special type of the , Different processes can be achieved through channel To interact with data .
channel There are two kinds of buffer and no buffer .
No buffer chan
If the current process has no buffer chan Send a message , But this chan When there is no ready receiving process , The current process will be blocked , Until a receiver collaboration is ready , And from chan Receive this message in .
There are buffers chan
After the buffer is filled , Try to chan The process of sending messages will be blocked . When the number of messages in the buffer is 0 when , Try from chan The process of reading messages in will be blocked .
channel The underlying implementation of
channel It is based on ring queue .
type hchan struct {
qcount uint // Number of elements remaining in the current queue
dataqsiz uint // Ring queue length ( The number of elements that can be stored )
buf unsafe.Pointer // Ring queue pointer
elemsize uint16 // Of each element ⼤⼩
closed uint32 // Identify the closed state
elemtype *_type // Element type
sendx uint // team 列 Subscript , Indicates where the element is stored in the queue when it is written x
recvx uint // Queue subscript , Indicates that the element is read from this position in the queue
recvq waitq // Queue of processes waiting to read messages
sendq waitq // The team waiting to write a message 列
lock mutex // The mutex ,chan Concurrent reading and writing are not allowed
}
The circular queue (buf)
from buf The pointer points to the ring queue , All access to chan All the data will be stored in the ring queue .
Waiting in line (recvq / sendq)
Waiting queue for reading messages (recvq):
If it's time to chan Buffer is empty , Or should chan There is no buffer , Then the current message reading process will be blocked . The blocked message reading process will be put into the message reading process waiting queue .Waiting queue for the process of writing messages (sendq):
If it's time to chan Buffer full , Or should chan There is no buffer , Then the current message writing process will be blocked . The blocked write message process will be put into the wait queue of the write message process .
Write data flow
picture source :https://www.jianshu.com/p/359a6e1ccd94
Read data flow
picture source :https://www.jianshu.com/p/359a6e1ccd94
Wake up the process
The process blocked by reading will be directed to chan Wake up by writing data .
The protocol blocked by writing will be removed from chan Wake up by reading data .
In general recvq and sendq At least one queue is empty . There is only one exception , That is, the same process uses select Statement to chan Read data while writing data .
channel Under what circumstances will Panic?
- To the closed chan Writing data will happen Panic
- Close the closed again chan It's going to happen Panic
- Close a value of nil Of chan It's going to happen Panic
Article reference :https://www.jianshu.com/p/359a6e1ccd94
边栏推荐
- ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
- Social media search engine optimization and its importance
- Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
- CorelDRAW graphics suite2022 free graphic design software
- 初学爬虫-笔趣阁爬虫
- Exposure X8 Standard Version picture post filter PS, LR and other software plug-ins
- June book news | 9 new books are listed, with a strong lineup and eyes closed!
- Markdown编辑语法
- Federal learning: dividing non IID samples according to Dirichlet distribution
- Embedded-c language-9-makefile/ structure / Consortium
猜你喜欢
cs架构下抓包的几种方法
Practical problem solving ability of steam Education
Federal learning: dividing non IID samples according to Dirichlet distribution
Websites that it people often visit
win11安装pytorch-gpu遇到的坑
Getting started with pytest ----- confitest Application of PY
缓存一致性解决方案——改数据时如何保证缓存和数据库中数据的一致性
農業生態領域智能機器人的應用
2022-003arts: recursive routine of binary tree
万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
随机推荐
数学知识——快速幂的理解及例题
win11安装pytorch-gpu遇到的坑
Exposure X8标准版图片后期滤镜PS、LR等软件的插件
汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
【ClickHouse】How to create index for Map Type Column or one key of it?
The core idea of performance optimization, dry goods sharing
unable to execute xxx. SH: operation not permitted
My first experience of shadowless cloud computer
Rhcsa --- work on the fourth day
解决:代理抛出异常错误
geotrust ov多域名ssl證書一年兩千一百元包含幾個域名?
C - derived classes and constructors
Leetcode merge sort linked list
Record the bug of unity 2020.3.31f1 once
Several methods of capturing packets under CS framework
奠定少儿编程成为基础学科的原理
C language practice - number guessing game
10 minute quick start UI automation ----- puppeter
[C language] basic learning notes
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing