How to understand coroutines and what are their application scenarios?
Looking at a few examples from Brother Niao, my understanding is that when yield is used in a function, a break point will be generated there. The next time you use this function, it will start running from this interruption point, but I don't know if my understanding is correct. . I don’t know what the application scenarios are like
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.