Understanding coroutines from a hardware perspective

son

In 2022, the year of the tiger is full of power~!

foreword

AndroidDevelopers are already familiar with Kotlinthe language , but there may be many students who don't understand itKotlin .协程

Read most of the articles on the web to get about 协程a few key words:

  • like threads;
  • is not a thread;
  • user state;
  • Collaborative;

Feeling very confused, I 协程just why there are so many strange and appropriate nouns.

Introduction to coroutines

Wikipedia: Coroutines

协程(英語:coroutine)A class of components of a computer program that promotes cooperative multitasking subroutines that allow execution to be suspended and resumed. Compared with subroutines, coroutines are more general and flexible, but in practice they are not as widely used as subroutines. Coroutines are more suitable for implementing familiar program components, such as cooperative multitasking, exception handling, event loops, iterators, infinite lists, and pipes.

computer physical hardware

协程Before we talk, let's talk about computer hardware-related knowledge.

Number of physical cpus

Refers to the number of cpuhardware ( socket). (However, this concept is often referred to as cpua number , which can easily lead to confusion with concepts such as corenumbers, processornumbers, etc., so the emphasis here is on physical cpunumbers ).

Since introducing multiple cpusockets requires more complex hardware support (connecting different sockets cputo memory and other resources), it is usually only done on servers. In a home computer, there is usually only one cpuslot .

number of cores

At the beginning, each physical cpucore has only one core a single core. For the operating system, only one process/thread can run at the same time. In order to improve performance, cpumanufacturers began to increase cores cpuon (real hardware exists), and dual-core cpu( dual-core cpu) and multi-core cpu( multiple cores) appeared. cpuSuch can run two processes/threads at the same time .

Hyper-Threading Technology

Simultaneous multithreading

Hyper-threading technology (hyper-threading/HT)

The essence is the same, it is a technology to increase the number of multi-threads that can be executed coreat the same (make full use of a coresingle computing power, try to make it "never idle for a moment").

simultaneous multithreadingThe abbreviation is SMT, AMDand other names of cpumanufacturers . hyper–threadingIt Intelis the title of , and can be considered hyper–threadingas a specific technical realization SMTof .

AMD-R7

Intel-i7

So it can be said like this: a certain model using SMTthe technology 4核心 AMD cpuprovides the ability to execute 8线程simultaneously ; a certain model using HTthe technology 2 核心 Intel cpuprovides 4 线程the ability to execute simultaneously.

Total logical cpunumber = physical cpunumber * number cpuof * number of hyperthreads per core

Threads and Coroutines

You must never fail to mention it when you 协程speak 线程.

线程It is the smallest unit that the operating system can perform calculations on.

In the general CPUcase, each core can only execute one thread at a time, except that the relatively new ones CPUhave the above-mentioned usage SMTor HTtechnology.

CPUBut 线程there is no necessary relationship between the number of cores and the number of . To give a very simple example, a piece of my code can always create 100 threads .

CPUIt doesn't understand which instructions it executes 线程, CPUand it doesn't need to understand them. It only needs the instructions assigned to it by the current operating system.

In the single-core CPUera all multithreading is actually multitasking, and multiple tasks are used alternately CPU资源.

With multi-core, tasks running on two threads can be truly parallelized, but the actual number of cores in the computer will never reach the number of tasks we need for computing. Therefore, the alternate use CPU资源of has always existed, but we know CPPthat switching the context of the execution thread consumes resources, and the more tasks, the higher the execution efficiency is not necessarily. Some suggestions for computationally intensive programs are to set the optimal number of threads to CPUExecutable Threads 1.5倍or 1倍+1.

At this time, we thought about whether we can not CPUswitch , which can reduce the waste of a lot of resources. CPUOr I/O操作let other routines execute first during long-term execution to improve resource utilization.

协程It was at this time that subroutines for cooperative multitasking were created.

At this time, we already 协程have a preliminary understanding of , let's think back to 协程the description of the 4 descriptions at the beginning of the article.

  • Like threads: During the execution of some programs, the concurrent execution of coroutines is the use of multi-threading technology (for example: no revisions have been made Java程序). So it's like a thread;
  • Not threads: The scheduling of concurrent tasks is not executed through thread switching at the operating system level, but the program itself supports multiple concurrent tasks of a single thread. So it can also be said that they are not threads, they can be called fibers Fiber, or green threads GreenThread. Just as a process can have multiple threads, a thread can also have multiple coroutines.
  • User mode: It is not a system-level thread and can execute asynchronous tasks autonomously. This kind of lightweight thread managed by the programmer's own program is called a user space thread , which has the characteristics of being invisible to the kernel.
  • Collaborative: Each running program is required to position itself to give up its execution rights, so that multiple tasks can be executed alternately. Wikipedia: Cooperative multitasking ;

Coroutines in Android

The above mentioned 协程reduces context switching and improves efficiency, so does Androidit kotlinsupport coroutines?

kotlinThe official document says: Essentially, coroutines are lightweight threads.

But Kotlin-JVMfor 协程it's threads. It is essentially a package based on Java Thread APInative .

Maybe Kotlinsubsequent versions will have real coroutine-related mechanisms instead of threads.

At this time, we may have some doubts. 协程Since Androidthere is still 线程no , Javaand both in Executorand Androidin AsyncTaskcan provide concurrent tasks, then what is the use kotlinof 协程it ? There will be an article explaining it separately~!

References:

Read an article to understand what are processes, threads, and coroutines

Are Kotlin coroutines really more efficient than Java threads?

Throwing the Line: A Glimpse of Coroutine Use Cases in Kotlin

The article is all told here, if you have other needs to communicate, you can leave a message~! ~!

If you want to read more articles by the author, you can check out my personal blog and public account:
Revitalize Book City

Guess you like

Origin blog.csdn.net/stven_king/article/details/122810591