[转]skynet Lua中的协程
Lua中的协程
http://www.outsky.org/code/lua-coroutine.html
Sep 6, 2014
Lua中的协程和其他变量一样,都是第一类值(first-class alue),可以被保存在变量中,可以被作为参数传递,可以被函数返回。
协程有4种状态:挂起(suspended),运行(running),死亡(dead)和正常(normal)。
Lua为协程提供了3个基础接口:create,resume和yield。
#coroutine.create
- 创建一个新的协程,并为它的运行分配一个独立的栈
- 协程处于挂起状态(suspended)
- 接受一个函数作为参数,这个函数就是协程的主程序块
- 返回这个协程
- 挂起点被设置为主程序块的第一句
#coroutine.resume
- 启动一个协程(第一次启动或从暂停状态启动)
- 自身(如果是协程的话)处于正常状态,被启动的协程处于运行状态
- 第一个参数为所要启动的协程
- 协程从它的挂起点开始执行
- 一直执行到被挂起或终止
- 导致协程终止的情况有两种:它的主程序块正常返回、运行过程中出错
- 执行结束后,控制权递交给此协程被激活的地方
#coroutine.yield
- 挂起一个协程
- 协程处于挂起状态
- 协程的运行状态被记录
- 激活它的那个coroutine.resume返回
#协程间通信
- 协程第一次被启动时,传递给coroutine.resume的参数将传递给协程的主程序
- 协程挂起时,传递给coroutine.yield的参数将作为上次启动它的coroutine.resume的返回值返回
- 协程被再次启动时,传递给coroutine.resume的参数将作为上次挂起它的coroutine.yield的返回值返回
- 协程死亡时,主程序返回的值将作为上次启动它的coroutine.resume的返回值返回
#实验
##状态
local function status(str, c)
print(str, coroutine.status(c))
end
local c1,c2
c1 = coroutine.create(function()
status("<c2>", c2)
print("before c1 yield")
coroutine.yield()
print("after c1 yield")
end)
c2 = coroutine.create(function()
status("<c2>", c2)
print("before c2 resume c1")
coroutine.resume(c1)
print("after c2 resume c1")
end)
status("<c2>", c2)
coroutine.resume(c2)
status("<c1>", c1)
status("<c2>", c2)
coroutine.resume(c1)
status("<c1>", c1)
输出:
outsky@x201:~/tmp$ lua test.lua
<c2> suspended
<c2> running
before c2 resume c1
<c2> normal
before c1 yield
after c2 resume c1
<c1> suspended
<c2> dead
after c1 yield
<c1> dead
##通信
local c = coroutine.create(function(...)
print("c start:", ...)
print("c yield return:", coroutine.yield("c yield"))
return "c dead"
end)
print("main start c")
local _,r = coroutine.resume(c, "main start c")
print("main resume return:", r)
print("--------")
print("main resume c")
_,r = coroutine.resume(c, "main resume c")
print("main resume return again:", r)
输出:
outsky@x201:~/tmp$ lua test.lua
main start c
c start: main start c
main resume return: c yield
--------
main resume c
c yield return: main resume c
main resume return again: c dead
[转]skynet Lua中的协程的更多相关文章
- lua中的协程
lua中的协程和线程类似: 1. 协程拥有自己的独立的栈,局部变量,和指令: 2. 所有协程都可以共享全局变量: 3. 协程不能像线程那样并行执行,协程之间需要相互协调执行,同一个时刻只能运行一个协程 ...
- Unity中的协程(一)
这篇文章很不错的问题,推荐阅读英文原版: Introduction to Coroutines Scripting with Coroutines 这篇文章转自:http://blog.csdn. ...
- 深入tornado中的协程
tornado使用了单进程(当然也可以多进程) + 协程 + I/O多路复用的机制,解决了C10K中因为过多的线程(进程)的上下文切换 而导致的cpu资源的浪费. tornado中的I/O多路复用前面 ...
- Lua 5.3 协程简单示例
Lua 5.3 协程简单示例 来源 http://blog.csdn.net/vermilliontear/article/details/50547852 生产者->过滤器->消费者 模 ...
- python中的协程及实现
1.协程的概念: 协程是一种用户态的轻量级线程.协程拥有自己的寄存器上下文和栈. 协程调度切换时,将寄存器上下文和栈保存到其他地方,在切换回来的时候,恢复先前保存的寄存器上下文和栈. 因此,协程能保留 ...
- fasthttp中的协程池实现
fasthttp中的协程池实现 协程池可以控制并行度,复用协程.fasthttp 比 net/http 效率高很多倍的重要原因,就是利用了协程池.实现并不复杂,我们可以参考他的设计,写出高性能的应用. ...
- Golang 入门系列(六)理解Go中的协程(Goroutine)
前面讲的都是一些Go 语言的基础知识,感兴趣的朋友可以先看看之前的文章.https://www.cnblogs.com/zhangweizhong/category/1275863.html. 今天就 ...
- python中的协程:greenlet和gevent
python中的协程:greenlet和gevent 协程是一中多任务实现方式,它不需要多个进程或线程就可以实现多任务. 1.通过yield实现协程: 代码: import time def A(): ...
- xlua 实现协程替换Unity中的协程
C#中的协程: IEnumerator ShowSpiritInfo() { UIMessageMgr.ShowMsgWait(true); DestroyUIModelInfo(); bool is ...
随机推荐
- jQuery生成二维码 jquery.qrcode.js
https://github.com/jeromeetienne/jquery-qrcode 1.将jquery.qrcode.min.js和jquery添加到您的网页中 <script src ...
- JedisClusterMaxRedirectionsException: Too many Cluster redirections
发生环境 当时的redis集群配置:redis-trib.rb 127.0.0.1 .... redis.conf的bind是默认# 解决方案 将redis.conf的bind为局域网真实ip red ...
- jqgrid取所有行的值,jqgrid取行对应列(name)的值,jqgrid取多行值对应列转json的方法
1.jqgrid取所有行的值(#gridTable指对应table的ID) var obj = $("#gridTable").jqGrid("getRowData&qu ...
- 水题系列二:PhoneNumbers
问题描述: Phonenumbers 企业喜欢用容易被记住的电话号码.让电话号码容易被记住的一个办法是将它写成一 个容易记 住的 单词或 者短语 .例如 ,你 需要给 滑铁卢 大学打 电话时 ,可 以 ...
- maven打包上传到本地中央库
pom文件中添加插件如下 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins< ...
- Redis在windows下的安装下载
1买个mac和台式电脑安装个Linux系统 2教程见:https://jingyan.baidu.com/article/0f5fb099045b056d8334ea97.html powerS ...
- ubuntu 挂载虚拟机vdi文件
sudo apt-get install nbd-server nbd-client qemu-kvm # rmmod nbd # modprobe nbd max_part=8 # qemu- ...
- std::string find 的返回值
std::string 的方法 find,返回值类型是std::string::size_type, 对应的是查找对象在字符串中的位置(从0开始), 如果未查找到,该返回值是一个很大的数据(4294 ...
- linux command mktemp
Linux command mktemp [Purpose] Learning linux command mktemp to create a temporary file or di ...
- 总结5条对学习Linux系统有帮助的经验心得
作为国产手机中的代表厂商,OPPO一直走在国内的前沿.不仅手机出货量在国内遥遥领先,而且在国外也抢占不少的市场份额.前段时间,OPPO在台湾地区签下田馥甄和林宥嘉担任OPPO R9s的代言人外,在东南 ...