当前位置:网站首页>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 ”
边栏推荐
- Multithreading Basics: basic concepts of threads and creation of threads
- Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
- Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
- 中缀表达式转后缀表达式详细思路及代码实现
- [paper notes] transunet: transformers make strongencoders for medical image segmentation
- [depth first search] Ji suanke: a joke of replacement
- First day of rhcsa study
- Dark horse -- redis
- Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion
- Human bone point detection: top-down (part of the theory)
猜你喜欢
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
C language daily practice - day 22: Zero foundation learning dynamic planning
[paper notes] transunet: transformers make strongencoders for medical image segmentation
Synchronous development of business and application: strategic suggestions for application modernization
Wx applet learning notes day01
The second day of rhcsa study
打家劫舍III[后序遍历与回溯+动态规划]
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
About static type, dynamic type, ID, instancetype
[depth first search] Ji suanke: a joke of replacement
随机推荐
Qlabel marquee text display
R语言使用order函数对dataframe数据进行排序、基于单个字段(变量)进行降序排序(DESCENDING)
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
ModuleNotFoundError: No module named ‘PIL‘解决方法
Camel case with Hungarian notation
安装Mysql报错:Could not create or access the registry key needed for the...
抽象类与抽象方法
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
青龙面板最近的库
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
Characteristic colleges and universities, jointly build Netease Industrial College
Black Horse - - Redis Chapter
helm部署etcd集群
Installation and management procedures
Airiot IOT platform enables the container industry to build [welding station information monitoring system]
使用map函数、split函数一行键入多个元素
test about BinaryTree
黑马--Redis篇
关于静态类型、动态类型、id、instancetype
AutoCAD - what is the default lineweight for centerline drawing and CAD? Can I modify it?