php - 协程是什么?有什么应用场景
给我你的怀抱
给我你的怀抱 2017-05-16 13:10:47
0
2
748

如何理解协程,协程有什么应用场景
从鸟哥这看几个例子,我的理解是在函数使用yield,就会在那产生中断点,下次使用这个函数时就会从这个中断点开始运行,不过不知道我的理解对不对。。还有应用场景不知道是怎样的

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!