当前位置:网站首页>通俗的讲解,带你入门协程
通俗的讲解,带你入门协程
2022-07-06 11:24:00 【MICAHHH】
线程
为什么要先讲线程,因为协程和线程很像。
我们都知道线程是CPU调度的最小单位,线程的存在就是为了以更小的开销实现并发,在线程执行时间片用完之后(时间片只是其中一种策略)将线程暂停,保存好线程的状态(上下文),然后转而执行其他线程。
线程的存在,让CPU不会浪费时间去等待某个线程的IO等操作。(毕竟CPU并不负责IO)
线程(甚至说进程)的调度是操作系统实现的,线程切换的实现一般放在内核,中断处理程序也是放在内核。 从这个角度来说,如果线程当前处于用户态,若要发生线程切换,必然是要先要先进入内核态,发生状态切换的。如果是用户级线程,那么在用户态直接切换线程的栈和寄存器,就不需要进入内核态。(整体开销就小)
协程与普通函数的区别
来看看一般的函数,函数只有一个出口。
再看看协程,可以类似这种函数(有多个出口和入口,还可以保存返回点,后续继续)
void func() {
print("a")
挂起(暂停并返回)
print("b")
挂起(暂停并返回)
print("c")
}
协程的底层实现
函数调用关系可以在栈上实现,是一个个栈帧的出栈入栈。但是协程看做函数的话,栈帧出栈后这个栈帧后续还要使用(只是被挂起),那么此时需要将栈帧弹出后保存在堆中。
协程和线程的区别
协程是一种在用户态进行调度的线程,所以也被称为用户态线程。一个线程开启后,协程是可以跑在这个线程上的,你可以创建多个协程,关系是一对多,只需要一个线程就可以管理很多协程,实现高并发。协程比线程要轻量很多,也能省去很多开销。
协程的历史
协程这种概念早在1958年就已经提出来了,要知道这时线程的概念都还没有提出来。到了1972年,终于有编程语言实现了这个概念,这两门编程语言就是Simula 67 以及Scheme。这一时期还没有线程,如果你想在操作系统写出并发程序那么你将不得不使用类似协程这样的技术,后来线程开始出现,操作系统终于开始原生支持程序的并发执行,就这样,协程逐渐淡出了程序员的视线。直到近些年,随着互联网的发展,尤其是移动互联网时代的到来,服务端对高并发的要求越来越高,协程再一次重回技术主流,各大编程语言都已经支持或计划开始支持协程。
协程的应用
常见提供原生协程支持的语言有:c++20、golang、python、rust 等,其他语言以库的形式提供协程功能,比如 C++20 之前腾讯的 fiber 和 libco、Java协程的类库quasar提供的纤程(其实就是协程)。
参考资料
边栏推荐
- QPushButton绑定快捷键的注意事项
- [depth first search] Ji suanke: a joke of replacement
- Qlabel marquee text display
- Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
- Interface test tool - postman
- The second day of rhcsa study
- R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
- AUTOCAD——中心线绘制、CAD默认线宽是多少?可以修改吗?
- 三年Android开发,2022疫情期间八家大厂的Android面试经历和真题整理
- English topic assignment (25)
猜你喜欢
php+redis实现超时取消订单功能
How to improve website weight
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
Druid 数据库连接池 详解
Analysis of frequent chain breaks in applications using Druid connection pools
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
[paper notes] transunet: transformers make strongencoders for medical image segmentation
About NPM install error 1
Online notes
安装Mysql报错:Could not create or access the registry key needed for the...
随机推荐
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the grouped dot strip plot, and set the add parameter to add box plots for different levels of dot strip
第五期个人能力认证考核通过名单公布
AcWing 3537. Tree lookup complete binary tree
Pytorch common loss function
RT-Thread 组件 FinSH 使用时遇到的问题
Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
能源行业的数字化“新”运维
QPushButton绑定快捷键的注意事项
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图
AIRIOT物联网平台赋能集装箱行业构建【焊接工位信息监控系统】
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
Airiot IOT platform enables the container industry to build [welding station information monitoring system]
五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
php+redis实现超时取消订单功能
Oracle advanced (IV) table connection explanation
The list of people who passed the fifth phase of personal ability certification assessment was published
A method of removing text blur based on pixel repair
裕太微冲刺科创板:拟募资13亿 华为与小米基金是股东
R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
Use map function and split function to type multiple elements in one line