当前位置:网站首页>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 ”
边栏推荐
- Looting iii[post sequence traversal and backtracking + dynamic planning]
- R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
- Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
- Oracle advanced (IV) table connection explanation
- R language uses DT function to generate t-distribution density function data and plot function to visualize t-distribution density function data
- 多线程基础:线程基本概念与线程的创建
- PMP每日一练 | 考试不迷路-7.6
- Take a look at how cabloyjs workflow engine implements activiti boundary events
- A method of removing text blur based on pixel repair
- 【论文笔记】TransUNet: Transformers Make StrongEncoders for Medical Image Segmentation
猜你喜欢
Computer network: sorting out common network interview questions (I)
渲大师携手向日葵,远控赋能云渲染及GPU算力服务
通俗的讲解,带你入门协程
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
ROS custom message publishing subscription example
CCNP Part 11 BGP (III) (essence)
受益匪浅,安卓面试问题
Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
helm部署etcd集群
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
随机推荐
Take a look at how cabloyjs workflow engine implements activiti boundary events
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置palette参数设置不同水平点阵图数据点和箱图的颜色
Wx applet learning notes day01
The second day of rhcsa study
渲大师携手向日葵,远控赋能云渲染及GPU算力服务
Characteristic colleges and universities, jointly build Netease Industrial College
Computer network: sorting out common network interview questions (I)
Interview assault 63: how to remove duplication in MySQL?
Dark horse -- redis
Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
test about BinaryTree
Benefit a lot, Android interview questions
Digital "new" operation and maintenance of energy industry
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
MRO工业品企业采购系统:如何精细化采购协同管理?想要升级的工业品企业必看!
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
反射及在运用过程中出现的IllegalAccessException异常
青龙面板最近的库