当前位置:网站首页>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
边栏推荐
- 初学爬虫-笔趣阁爬虫
- Common errors of dmrman offline backup
- Mysql表insert中文变?号的问题解决办法
- One step implementation of yolox helmet detection (combined with oak intelligent depth camera)
- Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
- Pytest learning ----- pytest assertion of interface automation testing
- Pytoch yolov5 runs bug solution from 0:
- 【ClickHouse】How to create index for Map Type Column or one key of it?
- Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
- Steam教育的实际问题解决能力
猜你喜欢

Mysql database learning

List of common bugs in software testing

Lay the foundation for children's programming to become a basic discipline

数学知识(欧拉函数)

Mysql表insert中文变?号的问题解决办法

Idea automatic package import and automatic package deletion settings

C language practice - number guessing game

Record the bug of unity 2020.3.31f1 once

解析少儿编程中的动手搭建教程

DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
随机推荐
Binary tree problem solving (1)
Alibaba cloud polkit pkexec local rights lifting vulnerability
C - derived classes and constructors
Common locks in MySQL
Summary of common string processing functions in C language
Exposure X8标准版图片后期滤镜PS、LR等软件的插件
6月书讯 | 9本新书上市,阵容强大,闭眼入!
Federal learning: dividing non IID samples according to Dirichlet distribution
Pytoch yolov5 runs bug solution from 0:
06 decorator mode
How to modify data file path in DM database
CY7C68013A之keil编译代码
培养中小学生对教育机器人的热爱之心
Use of Baidu map
win11安装pytorch-gpu遇到的坑
Analyzing the hands-on building tutorial in children's programming
Realize the function of data uploading
解决:代理抛出异常错误
Thinkphp內核工單系統源碼商業開源版 多用戶+多客服+短信+郵件通知
[C language] basic learning notes