Lua之协同程序coroutine代码实例
程序员文章站
2022-07-05 11:04:15
do
--create coroutine table
--coroutine state: suspended, running, dead, norma...
do --create coroutine table --coroutine state: suspended, running, dead, normal --when create the coroutine, the status is suspended, after calling it, the status is dead --get the coroutine status by the way coroutine.status local coa = 0; local cob = 0; function createcoroutinea() coa = coroutine.create( function() --for i = 0, 10 do print("coa: ", 0); print("cob status: ", coroutine.status(cob)); --normal status print("coa status: ", coroutine.status(coa)); print("coa coroutine next status"); --coroutine.yield();--the current coroutine is suspended --end end ); print("from coa to resume cob"); end function createcoroutineb() cob = coroutine.create( function() --for i = 0, 10 do print("cob: ", 0); print("coa status: ", coroutine.status(coa)); coroutine.resume(coa); --when resume coa, the cob will suspended, calling cob ,the coa status is --suspended and dead, this time will continue to execute the next code print("cob status: ", coroutine.status(cob)); print("cob coroutine next status"); --coroutine.yield(); --end end ); print("from cob to resume coa"); end --display the coa and cob status createcoroutinea(); print(coroutine.status(coa)); createcoroutineb(); print(coroutine.status(cob)); coroutine.resume(cob); print(coroutine.resume(cob)); --if the coroutine is dead ,the resume will resume false, and can't resume the dead coroutine --print("coa status: ", coroutine.status(coa)); --print("cob status: ", coroutine.status(cob)); end
注:
resume得到返回值,
如果有对应的yield在wait resume,那么yield的参数作为resum的返回值,第一个返回值表示coroutine没有错误,后面的返回值个数及其值视yeild参数而定。
如果没有yield在wait,那么返回值是对应函数的返回值,:true,* * *
do --create coroutine table --coroutine state: suspended, running, dead, normal --when create the coroutine, the status is suspended, after calling it, the status is dead --get the coroutine status by the way coroutine.status local coa = 0; local cob = 0; function createcoroutinea() coa = coroutine.create( function(parama, paramb) --for i = 0, 10 do print("coa: ", 0); coroutine.yield(parama, paramb);--the current coroutine is suspended --end return 100, 200; end ); print("from coa to resume cob"); end function createcoroutineb() cob = coroutine.create( function() --for i = 0, 10 do print("cob: ", 0); print("coa status: ", coroutine.status(coa)); coroutine.resume(coa); --when resume coa, the cob will suspended, calling cob ,the coa status is --suspended and dead, this time will continue to execute the next code print("cob status: ", coroutine.status(cob)); print("cob coroutine next status"); --coroutine.yield(); --end end ); print("from cob to resume coa"); end createcoroutinea(); --if not yield is waiting ,the return values that the main function return as the results of the resume --or the return as the yield params print( coroutine.resume(coa, 10, 20));--output:true, 10, 20 end
上一篇: 拼多多如何关闭消息提醒?
下一篇: Lua读写文件代码示例
推荐阅读
-
iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码
-
Java后缀数组之求sa数组的实例代码
-
IOS程序开发之禁止输入表情符号实例代码
-
基于zepto的插件之移动端无缝向上滚动并上下触摸滑动实例代码
-
Android项目实战之Glide 高斯模糊效果的实例代码
-
Android第三方登录之腾讯QQ登录的实例代码
-
vue.js树形组件之删除双击增加分支实例代码
-
SpringCloud之消息总线Spring Cloud Bus实例代码
-
spring学习之创建项目 Hello Spring实例代码
-
SpringCloud之服务注册与发现Spring Cloud Eureka实例代码