1 Symmetric Coroutine
Lua支持asymmetric coroutine。对于symmetric coroutine,名为Coroutines in Lua的一篇论文,提供了如下的解决方案:
coro = {}
coro.main = function() end
coro.current = coro.main
function coro.create(f)
return coroutine.wrap(function(val) return nil, f(val) end)
end
function coro.transfer(k, val)
if coro.current ~= coro.main then
return coroutine.yield(k, val)
else
while k do
coro.current = k
if k == coro.main then
return val
end
k, val = k(val)
end
error("coroutine ended without transfering control...")
end
end
代码虽然不长,但是十分精致。通过yield/resume,以及一个dipatching循环,巧妙地实现了symmetric coroutine。此外在Revisiting Coroutines论文中,甚至用asymmetric coroutine实现了one-shot continuation。
2 Reference
Coroutines in Lua
Ana L´ucia de Moura , Noemi Rodriguez , Roberto Ierusalimschy
Revisiting Coroutines
Ana L´ucia de Moura and Roberto Ierusalimschy
分享到:
相关推荐
9.1 Coroutine Basics 73 9.2 Pipes and Filters 76 9.3 Coroutines as Iterators 79 9.4 Non-Preemptive Multithreading 81 10 Complete Examples 87 10.1 Data Description 87 10.2 Markov Chain Algorithm ...
Lua Coroutine,协程详解,lua 基本础,The Programming Language Lua
test for lua coroutine and linux makecontext
1963], who described coroutines as “subroutines who act as the master program”, and implemented this construct to simplify the cooperation between the lexical and syntactical analysers in a COBOL ...
今天小编就为大家分享一篇关于Lua协同程序coroutine的简介及优缺点,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
支持在用户创建的lua coroutine中直接调用引擎提供的同步和异步api接口,对用户态coroutine执行不会产生任何影响;引擎会检测进程当中有效的lua服务总量,当服务总量为0时,引擎会自动安全退出(可以用node-lua作为...
Coroutine源码
Coroutine_Source_Test
主要介绍了Lua的协程(coroutine)简介,本文讲解了coroutine的创建、协程的三种状态和yield函数的配合使用等内容,需要的朋友可以参考下
年在巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro in Brazil)诞生了一门编程语言,发明者是该校的三位研究人员,他们给这门语言取了个浪漫的名字——Lua,在葡萄牙语里代表美丽的...
主要介绍了Lua协程(coroutine)程序运行分析,本文讲解分析了一段lua 协程代码是如何运行的,需要的朋友可以参考下
主要介绍了Lua之协同程序coroutine代码实例,本文给出的代码示例较为复杂,需要对Lua协同程序有一定的了解方能看懂,需要的朋友可以参考下
David Beazley作的关于Python并发coroutine的介绍slides
中关村 C#版本的LUA repo 主要是在 C# 中构建 LUA 5.2 的克隆,以便使具有脚本语言的 C# 项目能够扩展。 这包括 windows phone 项目、CoreCLR 项目。... 断言失败:big.lua nextvar.lua vararg.lua coroutine.lua
基于linux cpp实现的协程库coroutine
每个协程是一个独立的执行单元,为了能够方便协程之前的通信/同步,coroutine_event提供了chan这种机制。它本质上类似于一个阻塞消息队列,但是它不一定FIFO。它支持在多个线程里面的多个协程之间通信,但不同的线程...
Coroutine是基于Kilim/Promise JDeferred的协程式驱动框架,基于Apache Zookeeper的分布式规则存储和动态规则变更通知。 主要特性: 1. 基于微服务框架理念设计 2. 支持同步/异步调用 3. 支持串行/并行调用 4....
开源的c++协程实现,包含头文件即可使用详见https://github.com/tonbit/coroutine
这是一个不对称的协程库(如lua)。 您可以先使用coroutine_open打开一个计划,然后在该计划中创建coroutine。 您应该在调用coroutine_open的线程中调用coroutine_resume,并且不能在同一调度中的协程中调用它。 ...
安卓 Kotlin Coroutine协程 使用方式代码举例: 包含GlobalScope 、CoroutineScope 详细使用代码举例,相关介绍文章,可参考: https://xiaxl.blog.csdn.net/article/details/123383727