php - What are coroutines? What are the application scenarios?
给我你的怀抱
给我你的怀抱 2017-05-16 13:10:47
0
2
773

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

给我你的怀抱
给我你的怀抱

reply all(2)
曾经蜡笔没有小新

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以外的其它资源,还可以调用yieldGive up the CPU. Continue execution, so that you can write asynchronous code in a synchronous way.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template