[转]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 ...
随机推荐
- Leetcode 949. 给定数字能组成的最大时间
949. 给定数字能组成的最大时间 显示英文描述 我的提交返回竞赛 用户通过次数125 用户尝试次数213 通过次数127 提交次数774 题目难度Easy 给定一个由 4 位数字组成的数组,返 ...
- Node+Express的跨域访问控制问题:Access-Control-Allow-Origin
问题一:项目A通过Ajax访问项目B的接口,获取json数据,项目B采用Node+Express技术栈.项目A可能遇到跨域访问控制问题. 问题二:vue-resource 能够跨域,一般使用jsonp ...
- 循环中点击单个事件(巧用this,指向当前对象)
<em id='show' value="<?php echo $member['phone']; ?>" class="sui">&l ...
- Spring循环依赖
Spring-bean的循环依赖以及解决方式 Spring里面Bean的生命周期和循环依赖问题 什么是循环依赖? 循环依赖其实就是循环引用,也就是两个或者两个以上的bean互相持有对方,最终形成闭环. ...
- JavaScript 上万条数据 导出Excel文件 页面卡死
最近项目要js实现将数据导出excel文件,网上很多插件实现~~那个开心呀,谁知道后面数据量达到上万条时出问题:浏览器不仅卡死,导出的excel文件一直提示网络失败.... debug调试发现var ...
- Linux基线合规检查中各文件的作用及配置脚本
1./etc/motd 操作:echo " Authorized users only. All activity may be monitored and reported " ...
- Matlab函数
any() 相当于或操作,只要有1,就返回1 all() 相当于与操作,只要有0,就返回0 C = union(A,B): C为A和B的并集.去掉相同元素. C = intersect(A,B) C为 ...
- 【例子】log4j.properties例子讲解
log4j.rootLogger=info, ServerDailyRollingFile, stdout log4j.appender.ServerDailyRollingFile=org.apac ...
- python-列表,元组,range
# 列表# lst = ["光头强", 1, True, {}, (1, ), {123}, ["周杰伦",[], "周杰", " ...
- 遍历所有子物体中renderer(渲染器)中的material(材质)并改变其alpha值实现若隐若现的效果
using UnityEngine;using System.Collections;using UnityEngine.UI; public class CubeControl : MonoBeha ...