当前位置:网站首页>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 ”
边栏推荐
- 中缀表达式转后缀表达式详细思路及代码实现
- 打家劫舍III[后序遍历与回溯+动态规划]
- 三面蚂蚁金服成功拿到offer,Android开发社招面试经验
- Simple understanding of MySQL database
- Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
- 史上超级详细,想找工作的你还不看这份资料就晚了
- 使用map函数、split函数一行键入多个元素
- 朗坤智慧冲刺科创板:年营收4亿 拟募资7亿
- Camel case with Hungarian notation
- pytorch常见损失函数
猜你喜欢
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
三年Android开发,2022疫情期间八家大厂的Android面试经历和真题整理
史上超级详细,想找工作的你还不看这份资料就晚了
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
安装Mysql报错:Could not create or access the registry key needed for the...
裕太微冲刺科创板:拟募资13亿 华为与小米基金是股东
Oracle advanced (IV) table connection explanation
今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
How word displays modification traces
如何提高网站权重
随机推荐
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
An error occurs when installing MySQL: could not create or access the registry key needed for the
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
Don't miss this underestimated movie because of controversy!
Human bone point detection: top-down (part of the theory)
MRO工业品企业采购系统:如何精细化采购协同管理?想要升级的工业品企业必看!
裕太微冲刺科创板:拟募资13亿 华为与小米基金是股东
QPushButton绑定快捷键的注意事项
Airiot IOT platform enables the container industry to build [welding station information monitoring system]
Camel case with Hungarian notation
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
How to type multiple spaces when editing CSDN articles
Installation and management procedures
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
The list of people who passed the fifth phase of personal ability certification assessment was published
业务与应用同步发展:应用现代化的策略建议
Excel 中VBA脚本的简单应用
Digital "new" operation and maintenance of energy industry