当前位置:网站首页>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
边栏推荐
- Summary of common string processing functions in C language
- Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
- Geotrust OV Multi - Domain Domain SSL Certificate rmb2100 per year contains several Domain names?
- Idea automatic package import and automatic package deletion settings
- Cannot activate CONDA virtual environment in vscode
- Alibaba cloud polkit pkexec local rights lifting vulnerability
- Design and implementation of general interface open platform - (44) log processing of API services
- 阿里云polkit pkexec 本地提权漏洞
- Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
- idea自動導包和自動删包設置
猜你喜欢
![[C language] Dynamic Planning --- from entry to standing up](/img/7e/29482c8f3970bb1a40240e975ef97f.png)
[C language] Dynamic Planning --- from entry to standing up

CorelDRAW Graphics Suite2022免费图形设计软件

Lm09 Fisher inverse transform inversion mesh strategy

win11安装pytorch-gpu遇到的坑

Let正版短信测压开源源码

Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知

Mysql database learning

C language practice - binary search (half search)

unable to execute xxx. SH: operation not permitted

Idea autoguide package and autodelete package Settings
随机推荐
Practical problem solving ability of steam Education
Detailed process of DC-1 range construction and penetration practice (DC range Series)
Hcip day 17
农业生态领域智能机器人的应用
Embedded-c language-9-makefile/ structure / Consortium
Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
Leetcode merge sort linked list
CorelDRAW Graphics Suite2022免费图形设计软件
Analyze the space occupied by the table according to segments, clusters and pages
Pytest learning ----- pytest assertion of interface automation testing
Promise all()
Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
Read "the way to clean code" - function names should express their behavior
【ClickHouse】How to create index for Map Type Column or one key of it?
cs架构下抓包的几种方法
Pytorch-Yolov5從0運行Bug解决:
LeetCode-归并排序链表
AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
Design and implementation of general interface open platform - (44) log processing of API services
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification