发布于 2015-07-26 14:52:48 | 273 次阅读 | 评论: 0 | 来源: 网络整理

协同程序是协同的性质,可以把两个或更多的方法以可控制的方式执行。随着协同程序,在任何给定的时间,只有其协同程序运行之一,这在运行协同程序只能暂停其执行时,明确要求暂停。

上述定义可能看起来模糊。来告诉它更清楚,假设我们有两个方法,一个主程序方法和协同程序。当我们使用恢复功能调用协程,其开始执行,当我们调用yield功能,暂停执行。再次同协程可以继续从它被暂停的另一个恢复功能调用执行。这个过程可以继续,直到执行了协程的结束。

协同程序可用的功能

下表列出了在Lua协同程序及其相应的使用所有的可用功能。

S.N. Method & Purpose
1. coroutine.create (f): 
创建一个新的协程与函数f 和 返回类型“线程”的对象。
2. coroutine.resume (co [, val1, ...]): 
重新开始corountine co和传递的参数(若有)。它返回操作和可选的其他返回值的状态。
3. coroutine.running (): 
返回运行的协同程序或零,如果调用主线程。
4. coroutine.status (co): 
返回运行,正常其中一个值,暂停或死亡的基础上,协程的状态。
5. coroutine.wrap (f): 
像coroutine.create,coroutine.wrap函数还创建了一个协同程序,但不是返回协同程序本身,它返回一个函数被调用时,将恢复协程。
6. coroutine.yield (...): 
暂停运行协程。传递给该方法的参数作为附加的返回值以恢复功能。

例子

让我们看一个例子就明白了协程的概念。


co = coroutine.create(function (value1,value2)
   local tempvar3 =10
   print("coroutine section 1", value1, value2, tempvar3)
   local tempvar1 = coroutine.yield(value1+1,value2+1)
   tempvar3 = tempvar3 + value1
   print("coroutine section 2",tempvar1 ,tempvar2, tempvar3)
   local tempvar1, tempvar2= coroutine.yield(value1+value2, value1-value2)
   tempvar3 = tempvar3 + value1
   print("coroutine section 3",tempvar1,tempvar2, tempvar3)
   return value2, "end"
end)

print("main", coroutine.resume(co, 3, 2))
print("main", coroutine.resume(co, 12,14))
print("main", coroutine.resume(co, 5, 6))
print("main", coroutine.resume(co, 10, 20))

当我们运行上面的程序,会得到下面的输出。


coroutine section 1	3	2	10
main	true	4	3
coroutine section 2	12	nil	13
main	true	5	1
coroutine section 3	5	6	16
main	true	2	end
main	false	cannot resume dead coroutine

上面的例子是做什么?

如之前所提到的,我们使用恢复功能的动作开始,并产生函数来停止操作。此外,可以看到有由协程恢复功能接收多个返回值。这里将解释上面的程序每一个步骤,使之清楚。

  • 首先,我们创建了一个协同程序,并把它分配给变量名合作和协同程序需要在两个变量作为参数。

  • 当我们称之为第一恢复功能,值3和2分别被保持在临时变量value1和value2,直到协程的结束。

  • 为了理解这一点,我们已经使用了tempvar3初始化为10,它被由协程的后续调用更新为13和16,因为值1被保留为3,整个协同程序的执行。

  • 第一个coroutine.yield返回两个值4和3 是由更新输入参数3和2,yield语句得到了恢复函数。它还接收协程执行的真/假状态。

  • 关于协程的另一件事是如何恢复调用下一参数写成的照顾,在上述的例子; 可以看到,coroutine.yield分配变量接收到下一次调用参数,它提供做新业务与现有参数值之间的关系的一种强有力的方式。

  • 最后,一旦在协同程序的所有语句执行时,后续调用将返回false,并且“不能恢复死协同程序”语句作为回应。

另一个协程的例子

让我们来看一个简单的协同程序返回一个数字,从1到5 yield函数恢复功能。它创建协同程序,如果没有则恢复现有的协程。


function getNumber()
   local function getNumberHelper()
      co = coroutine.create(function ()
      coroutine.yield(1)
      coroutine.yield(2)
      coroutine.yield(3)
      coroutine.yield(4)
      coroutine.yield(5)
      end)
      return co
   end
   if(numberHelper) then
      status, number = coroutine.resume(numberHelper);
      if coroutine.status(numberHelper) == "dead" then
         numberHelper = getNumberHelper()
         status, number = coroutine.resume(numberHelper);
      end
      return number
   else
      numberHelper = getNumberHelper()
      status, number = coroutine.resume(numberHelper);
      return number
   end
end
for index = 1, 10 do
   print(index, getNumber())
end

当我们运行上面的程序,会得到下面的输出。


1	1
2	2
3	3
4	4
5	5
6	1
7	2
8	3
9	4
10	5

往往有协同程序与多道程序语言的线程的比较,但要明白,协同程序线程有类似的功能,但只有一次执行,并不会执行兼任。

我们控制程序的执行顺序,以满足与提供暂时保留某些信息的需求。使用全局变量与协程,提供了协同程序更加灵活。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务