Regarding coroutines, the most common thing you may read is this sentence: "Coroutines are user-mode threads".
To understand what a "user-mode thread" is, you must first understand what a "kernel-mode thread" is. Kernel state threads are scheduled by the operating system. When switching thread contexts, you must first save the context of the previous thread, and then execute the next thread. When the conditions are met, switch back to the previous thread and restore the context. The same is true for coroutines, except that user-mode threads are not scheduled by the operating system, but by programmers, in user-mode.
yield这个关键字就是用来产生中断, 并保存当前的上下文的, 比如说程序的一段代码是访问远程服务器,那这个时候CPU就是空闲的,就用yield让出CPU,接着执行下一段的代码,如果下一段代码还是访问除CPU以外的其它资源,还可以调用yieldGive up the CPU. Continue execution, so that you can write asynchronous code in a synchronous way.
Understanding of processes, threads and coroutines
Regarding coroutines, the most common thing you may read is this sentence: "Coroutines are user-mode threads".
To understand what a "user-mode thread" is, you must first understand what a "kernel-mode thread" is. Kernel state threads are scheduled by the operating system. When switching thread contexts, you must first save the context of the previous thread, and then execute the next thread. When the conditions are met, switch back to the previous thread and restore the context. The same is true for coroutines, except that user-mode threads are not scheduled by the operating system, but by programmers, in user-mode.
yield
这个关键字就是用来产生中断, 并保存当前的上下文的, 比如说程序的一段代码是访问远程服务器,那这个时候CPU就是空闲的,就用yield
让出CPU,接着执行下一段的代码,如果下一段代码还是访问除CPU以外的其它资源,还可以调用yield
Give up the CPU. Continue execution, so that you can write asynchronous code in a synchronous way.