当前位置:网站首页>A popular explanation will help you get started

A popular explanation will help you get started

2022-07-06 19:19:00 MICAHHH

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 .
 Insert picture description here
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")
}

 Insert picture description here

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 .
 Insert picture description here

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

  1. Will intra process thread switching trigger the switching between kernel state and user state ? - DearFuture Answer - You know
  2. A popular explanation of the difference between threads and coroutines
  3. Manon's island for survival 《 How should programmers understand coroutines in high concurrency 》
  4. A brief introduction to the project (coroutine)
  5. Java Medium “ coroutines ”
原网站

版权声明
本文为[MICAHHH]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061124036072.html

随机推荐