当前位置:网站首页>A popular explanation will help you get started
A popular explanation will help you get started
2022-07-06 19:19:00 【MICAHHH】
Catalog
Threads
Why talk about threads first , Because coroutines are very similar to threads .
We all know that threads are CPU Minimum unit of scheduling , Threads exist to achieve concurrency with less overhead , After the thread execution time slice runs out ( Time slice is just one of the strategies ) Pause the thread , Save the state of the thread ( Context ), Then switch to other threads .
The existence of threads , Give Way CPU Don't waste time waiting for a thread IO Wait for the operation .( After all CPU Not responsible IO)
Threads ( Even the process ) The scheduling of is implemented by the operating system , The implementation of thread switching is generally placed in the kernel , Interrupt handlers are also placed in the kernel . From this perspective , If the thread is currently in user mode , To switch threads , It must first enter the kernel state , State switching occurs . If it is a user level thread , Then switch the stack and register of the thread directly in user mode , There is no need to enter the kernel state .( The overall cost is small )
The difference between CO process and ordinary function
Let's look at general functions , Function has only one exit .
Take another look at Xiecheng , You can use functions like this ( There are multiple exits and entrances , You can also save the return point , Go ahead )
void func() {
print("a")
Hang up ( Pause and return )
print("b")
Hang up ( Pause and return )
print("c")
}

The bottom implementation of the collaboration
Function call relationships can be implemented on the stack , Is a stack of frames out of the stack into the stack . But if the covariance is regarded as a function , After the stack frame comes out of the stack, this stack frame will be used later ( Just suspended ), At this time, you need to pop up the stack frame and save it in the heap .
The difference between a process and a thread
A coroutine is a thread that schedules in user mode , So it is also called user thread . After a thread is started , A coroutine can run on this thread , You can create multiple collaborations , The relationship is one to many , Only one thread is needed to manage many coroutines , Achieve high concurrency . Coroutines are much lighter than threads , It can also save a lot of expenses .
The history of Xiecheng
The concept of synergy dates back to 1958 It's been put forward since 2000 , You know, the concept of thread has not been put forward yet . here we are 1972 year , Finally, a programming language has implemented this concept , These two programming languages are Simula 67 as well as Scheme. There are no threads in this period , If you want to write concurrent programs on the operating system, you will have to use techniques like coroutines , Then threads began to appear , The operating system finally started to support concurrent execution of native programs , That's it , Xiecheng gradually faded out of the sight of programmers . Until recently , With the development of the Internet , Especially with the advent of the mobile Internet era , The requirement of server for high concurrency is higher and higher , Xiecheng is back in the mainstream of technology once again , Major programming languages have supported or plan to start supporting coprocessing .
Applications of coroutines
Common languages that provide native collaboration support are :c++20、golang、python、rust etc. , Other languages provide coroutine functions in the form of Libraries , such as C++20 Previous Tencent fiber and libco、Java Class library of collaboration quasar Fiber path provided ( In fact, it is Xiecheng ).
Reference material
- Will intra process thread switching trigger the switching between kernel state and user state ? - DearFuture Answer - You know
- A popular explanation of the difference between threads and coroutines
- Manon's island for survival 《 How should programmers understand coroutines in high concurrency 》
- A brief introduction to the project (coroutine)
- Java Medium “ coroutines ”
边栏推荐
- Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
- 使用map函数、split函数一行键入多个元素
- 第五期个人能力认证考核通过名单公布
- Excel 中VBA脚本的简单应用
- Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
- MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
- 业务与应用同步发展:应用现代化的策略建议
- swagger2报错Illegal DefaultValue null for parameter type integer
- 黑馬--Redis篇
- 如何提高网站权重
猜你喜欢

About static type, dynamic type, ID, instancetype

Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
![Estimate blood pressure according to PPG using spectral spectrum time depth neural network [turn]](/img/f2/194ae452609d3e474b7b580d61bf4f.png)
Estimate blood pressure according to PPG using spectral spectrum time depth neural network [turn]

About NPM install error 1

LeetCode-1279. Traffic light intersection

五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”

助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!

Take a look at how cabloyjs workflow engine implements activiti boundary events

基于蝴蝶种类识别

How word displays modification traces
随机推荐
[depth first search] Ji suanke: Square
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图
Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图
R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the palette parameter, and set the colors of data points and box graphs of dot plots at differ
Precautions for binding shortcut keys of QPushButton
R language uses rchisq function to generate random numbers that conform to Chi square distribution, and uses plot function to visualize random numbers that conform to Chi square distribution
Optical blood pressure estimation based on PPG and FFT neural network [translation]
ACTF 2022圆满落幕,0ops战队二连冠!!
About NPM install error 1
Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion
史上超级详细,想找工作的你还不看这份资料就晚了
When visual studio code starts, it prompts "the code installation seems to be corrupt. Please reinstall." Solution to displaying "unsupported" information in the title bar
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
驼峰式与下划线命名规则(Camel case With hungarian notation)