您的位置:首页 > 其它

用协同程序实现生产者消费者问题

2018-02-06 14:29 190 查看
local a

  productor = function()

      local i = 0 

      while true do

          i = i + 1 

          send(i)

      end 

  end  

  

  send = function(i)

      coroutine.yield(i)

  end

  

  recevie = function()

      local status, i = coroutine.resume(a)

      return i

  end

  

  

  func = function()

      local i = 0 

      while (i < 20) do

          i = recevie()

          print(i)

      end 

  end

  

  

  a = coroutine.create(productor)

  func()

运行结果

请按 ENTER 或其它命令继续

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: