当前位置:  首页>> 技术小册>> Lua入门教程

什么是协同(coroutine)?

Lua 协同程序(coroutine)与线程比较类似:拥有独立的堆栈,独立的局部变量,独立的指令指针,同时又与其它协同程序共享全局变量和其它大部分东西。

协同是非常强大的功能,但是用起来也很复杂。

线程和协同程序区别

线程与协同程序的主要区别在于,一个具有多个线程的程序可以同时运行几个线程,而协同程序却需要彼此协作的运行。

在任一指定时刻只有一个协同程序在运行,并且这个正在运行的协同程序只有在明确的被要求挂起的时候才会被挂起。

协同程序有点类似同步的多线程,在等待同一个线程锁的几个线程有点类似协同。

基本语法

  1. 方法 描述
  2. coroutine.create() 创建 coroutine,返回 coroutine 参数是一个函数,当和 resume 配合使用的时候就唤醒函数调用
  3. coroutine.resume() 重启 coroutine,和 create 配合使用
  4. coroutine.yield() 挂起 coroutine,将 coroutine 设置为挂起状态,这个和 resume 配合使用能有很多有用的效果
  5. coroutine.status() 查看 coroutine 的状态
  6. 注:coroutine 的状态有三种:deadsuspendedrunning,具体什么时候有这样的状态请参考下面的程序
  7. coroutine.wrap() 创建 coroutine,返回一个函数,一旦你调用这个函数,就进入 coroutine,和 create 功能重复
  8. coroutine.running() 返回正在跑的 coroutine,一个 coroutine 就是一个线程,当使用running的时候,就是返回一个 coroutine 的线程号

以下实例演示了以上各个方法的用法:
coroutine_test.lua 文件

  1. -- coroutine_test.lua 文件
  2. co = coroutine.create(
  3. function(i)
  4. print(i);
  5. end
  6. )
  7. coroutine.resume(co, 1) -- 1
  8. print(coroutine.status(co)) -- dead
  9. print("----------")
  10. co = coroutine.wrap(
  11. function(i)
  12. print(i);
  13. end
  14. )
  15. co(1)
  16. print("----------")
  17. co2 = coroutine.create(
  18. function()
  19. for i=1,10 do
  20. print(i)
  21. if i == 3 then
  22. print(coroutine.status(co2)) --running
  23. print(coroutine.running()) --thread:XXXXXX
  24. end
  25. coroutine.yield()
  26. end
  27. end
  28. )
  29. coroutine.resume(co2) --1
  30. coroutine.resume(co2) --2
  31. coroutine.resume(co2) --3
  32. print(coroutine.status(co2)) -- suspended
  33. print(coroutine.running())
  34. print("----------")

以上实例执行输出结果为:

  1. 1
  2. dead
  3. ----------
  4. 1
  5. ----------
  6. 1
  7. 2
  8. 3
  9. running
  10. thread: 0x7fb801c05868 false
  11. suspended
  12. thread: 0x7fb801c04c88 true
  13. ----------

coroutine.running就可以看出来,coroutine在底层实现就是一个线程。

当create一个coroutine的时候就是在新线程中注册了一个事件。

当使用resume触发事件的时候,create的coroutine函数就被执行了,当遇到yield的时候就代表挂起当前线程,等候再次resume触发事件。

接下来我们分析一个更详细的实例:

实例

  1. function foo (a)
  2. print("foo 函数输出", a)
  3. return coroutine.yield(2 * a) -- 返回 2*a 的值
  4. end
  5. co = coroutine.create(function (a , b)
  6. print("第一次协同程序执行输出", a, b) -- co-body 1 10
  7. local r = foo(a + 1)
  8. print("第二次协同程序执行输出", r)
  9. local r, s = coroutine.yield(a + b, a - b) -- ab的值为第一次调用协同程序时传入
  10. print("第三次协同程序执行输出", r, s)
  11. return b, "结束协同程序" -- b的值为第二次调用协同程序时传入
  12. end)
  13. print("main", coroutine.resume(co, 1, 10)) -- true, 4
  14. print("--分割线----")
  15. print("main", coroutine.resume(co, "r")) -- true 11 -9
  16. print("---分割线---")
  17. print("main", coroutine.resume(co, "x", "y")) -- true 10 end
  18. print("---分割线---")
  19. print("main", coroutine.resume(co, "x", "y")) -- cannot resume dead coroutine
  20. print("---分割线---")

以上实例执行输出结果为:

  1. 第一次协同程序执行输出 1 10
  2. foo 函数输出 2
  3. main true 4
  4. --分割线----
  5. 第二次协同程序执行输出 r
  6. main true 11 -9
  7. ---分割线---
  8. 第三次协同程序执行输出 x y
  9. main true 10 结束协同程序
  10. ---分割线---
  11. main false cannot resume dead coroutine
  12. ---分割线---

以上实例接下如下:

调用resume,将协同程序唤醒,resume操作成功返回true,否则返回false;
协同程序运行;
运行到yield语句;
yield挂起协同程序,第一次resume返回;(注意:此处yield返回,参数是resume的参数)
第二次resume,再次唤醒协同程序;(注意:此处resume的参数中,除了第一个参数,剩下的参数将作为yield的参数)
yield返回;
协同程序继续运行;
如果使用的协同程序继续运行完成后继续调用 resume方法则输出:cannot resume dead coroutine
resume和yield的配合强大之处在于,resume处于主程中,它将外部状态(数据)传入到协同程序内部;而yield则将内部的状态(数据)返回到主程中。

生产者-消费者问题

现在我就使用Lua的协同程序来完成生产者-消费者这一经典问题。

实例

  1. local newProductor
  2. function productor()
  3. local i = 0
  4. while true do
  5. i = i + 1
  6. send(i) -- 将生产的物品发送给消费者
  7. end
  8. end
  9. function consumer()
  10. while true do
  11. local i = receive() -- 从生产者那里得到物品
  12. print(i)
  13. end
  14. end
  15. function receive()
  16. local status, value = coroutine.resume(newProductor)
  17. return value
  18. end
  19. function send(x)
  20. coroutine.yield(x) -- x表示需要发送的值,值返回以后,就挂起该协同程序
  21. end
  22. -- 启动程序
  23. newProductor = coroutine.create(productor)
  24. consumer()

以上实例执行输出结果为:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. ……

该分类下的相关小册推荐: