Lua coroutines coroutine study notes _

Return Value cooperative program

  Can be written in (the return value, return value .....) coroutine.yield , or directly return return value, return value .....

  coroutine.yield and coroutine.resume all can have multiple return values!

  coroutine.resume first return value used to indicate whether they were successful coroutine; from the second onward return value is coroutine.yield returned .

 

  coroutine.yield return value is coroutine.resume passed, i.e. coroutine.resume the parameters (except the first argument).

 


Data on the internal and external collaboration program (main routine) AC

  Here is a rookie tutorial code, and the results of the implementation:

. 1  function foo (A)
 2      Print ( " foo function output " , A)
 . 3      return  to coroutine.yield ( 2 * A) - returns the value of 2 * a 
. 4  End 
. 5   
. 6 CO.'S = to coroutine.create ( function (A, B )
 . 7      Print ( " first output coroutine execution " , A, B) - CO.'s body. 1-10 
. 8      local R & lt foo = (A + . 1 )
 . 9       
10      Print ( " second output coroutine execution ", R & lt)
 . 11      local R & lt, = S to coroutine.yield (A + b, A - b)   - A, when the value of b is an incoming call to the first cooperative program 
12 is       
13 is      Print ( " third output coroutine execution ' , R & lt, S)
 14      return b, " end coroutine "                    - incoming b value when the second call cooperative program 
15  end )
 16         
. 17  Print ( " main " , to coroutine.resume (CO.'s, . 1 , 10 )) - to true,. 4 
18 is  Print ( " - dividing line ----")
19 print("main", coroutine.resume(co, "r")) -- true 11 -9
20 print("---分割线---")
21 print("main", coroutine.resume(co, "x", "y")) -- true 10 end
22 print("---分割线---")
23 print(" Main " , to coroutine.resume (CO.'S, " X " , " Y " )) - CAN Not Dead Resume the coroutine 
24  Print ( " --- --- parting line " )
 25  
26 is  the execution result:
 27 first coroutine perform output     . 1     10 
28 foo function output     2 
29 main     to true     . 4 
30  - parting line ---- 
31 is  a second output coroutine execution R & lt
 32 main     to true     . 11     - . 9 
33 is - - --- dividing line 
34 is  a third output coroutine execution XY
 35 main     to true     10     ends coroutine
 36  - - dividing line --- 
37 [ main     to false     CAN Not Dead Resume the coroutine
 38 is  - - dividing line ---
View Code

 

On line 10 of the output is " second collaborative program execution output r ", felt puzzled:

   r not in the time line 8, assigned coroutine.yield ( 2 * A) yet? And to coroutine.yield ( 2 * A) of the return value is fishes, the output should be " the second output 4 coroutine execution ."

 

After pondering, I understand this is why:

  In the second row. 3 return to coroutine.yield ( 2 * A) returned value is not assigned to r ; the current but directly out of the coroutine to return to the first row. 17 to coroutine.resume at the print out.

 The program continues to go down, starting at line 19, coroutine.resume passed the "r" :

  Into the coroutine is executed to line 3, line 3 return to coroutine.yield ( 2 * A) , this time to the real value of the return, the return value is passed to "r".

 end function foo, local R & lt value of the return value of the function foo "R & lt" .

 

Word on the rookie tutorial:

  And resume power of the yield is complex in that, in the main resume process will external state (data) is passed to the internal coordination program; will yield the interior of the state (data) is returned to the main process.

 

My own conclusion:

  When the yield inside coroutine execution, return parameters passed to the outside in the resume, as part of the value returned resume (return value from the beginning of the second) .

  Resume execution of external, will pass from the beginning of the second parameter to the interior of the coroutine yield, as the return value of yield .

 

 

To be added...

Guess you like

Origin www.cnblogs.com/guoyujam/p/12219289.html