当前位置:网站首页>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
边栏推荐
- Use of typescript classes
- CorelDRAW Graphics Suite2022免费图形设计软件
- LM09丨费雪逆变换反转网格策略
- Oracle stored procedure and job task setting
- Mysql表insert中文变?号的问题解决办法
- [understand one article] FD_ Use of set
- Federal learning: dividing non IID samples according to Dirichlet distribution
- Solution of DM database unable to open graphical interface
- 農業生態領域智能機器人的應用
- UNET deployment based on deepstream
猜你喜欢
VMware installation win10 reports an error: operating system not found
数学问题(数论)试除法做质数的判断、分解质因数,筛质数
Federal learning: dividing non IID samples according to Dirichlet distribution
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
CY7C68013A之keil编译代码
LeetCode-对链表进行插入排序
C language practice - binary search (half search)
06 装饰(Decorator)模式
Application of intelligent robot in agricultural ecology
What methods should service define?
随机推荐
6月书讯 | 9本新书上市,阵容强大,闭眼入!
How to write a client-side technical solution
Lay the foundation for children's programming to become a basic discipline
Pit encountered in win11 pytorch GPU installation
06 装饰(Decorator)模式
Oracle stored procedure and job task setting
Unity particle Foundation
CorelDRAW Graphics Suite2022免费图形设计软件
DJB Hash
Summary of common string processing functions in C language
Leetcode- insert and sort the linked list
数学问题(数论)试除法做质数的判断、分解质因数,筛质数
Detailed process of DC-1 range construction and penetration practice (DC range Series)
C language practice - number guessing game
Rhcsa --- work on the third day
培养中小学生对教育机器人的热爱之心
关于Steam 教育的知识整理
Deep understanding of lambda expressions
Comp 250 parsing
Summary of main account information of zhengdaliu 4