This is a section of the code that analyzes the LUA coprocessor (collaboration program, Coroutine) from the LUA Reference manual interface (slightly modified):
Copy Code code as follows:
function foo (a)
Print ("foo", a)
Return Coroutine.yield (2*a)
End
CO = coroutine.create (function (a,b)
Print ("Co-body1", A, B)
Local r = foo (a+1)
Print ("Co-body2", R)
Local r, s = Coroutine.yield (A+b, A-b)
Print ("Co-body3", R, s)
return B, "End"
End
Print ("1----")
Print ("Main", Coroutine.resume (Co, 1, 10))
Print ("2----")
Print ("Main", Coroutine.resume (CO, "R"))
Print ("3----")
Print ("Main", Coroutine.resume (Co, "X", "Y"))
Print ("4----")
Print ("Main", Coroutine.resume (Co, "X", "Y"))
The operation effect is as follows:
Copy Code code as follows:
1------
Co-body1 1 10
Foo 2
Main true 4
2------
Co-body2 R
Main true 11-9
3------
Co-body3 x y
Main true Ten End
4------
Main false cannot resume dead coroutine
Here we call 4 times resume, let's see how it works.
First time:
Copy Code code as follows:
Print ("Main", Coroutine.resume (Co, 1, 10))
1. Execute print ("Co-body1", A, b), the values of A and B are provided for resume, a=1, b=10;
2. Calculate a+1=2, enter foo (a), at the same time pass the result of the calculation passed through a parameter, execute print ("foo", a);
3. Consider return Coroutine.yield (2*a);
4. Calculate 2*a=4, touch yield, suspend foo (a) call, return 4 to resume. Note that Foo's return has not been implemented;
5.resume execution succeeded, returns True, 4.
Second time:
Copy Code code as follows:
Print ("Main", Coroutine.resume (CO, "R"))
1. Start execution from the last pending foo (a) call, followed by a return call that is not completed;
2. The value returned by foo (a+1) is the string "r" because the yield returns the call parameter of the resume. It's more difficult to understand here.
Because you might logically assume that the value of the local R variable should be the value of the 2*a in Yield (2*a).
It should be noted that the return value of the yield is different from the value of the yield parameter.
The former you can save it in a variable, or return it, or do not use it (do not save yield return results), the latter is the resume value.
3. Execute print ("Co-body2", R), R with the value "R";
4. Consider local r, s = Coroutine.yield (a+b, a-b);
5. Calculate a+b=11, a-b=-9, encounter yield, suspend the call of CO, return 11 and 9 to resume. Note that the assignment of local R, S is not yet started.
What's not very well understood here is why the value of a is not "R"? Because "R" has been consumed by the return value of the above yield.
6.resume execution succeeded, returns True, 11,-9.
Third time:
Copy Code code as follows:
Print ("Main", Coroutine.resume (Co, "X", "Y"))
1. Start execution from the last yield, then execute the incomplete local r, S = Assignment. As mentioned above, yield returns the invocation parameters of resume, so the value of R and S is "X" and "Y";
2. Execute print ("Co-body3", R, s) for printing;
3. Consider return B, "end";
The value of 4.b has always been 10 unchanged, here directly returned, and also returned the "End" of this string;
5. Since the covariant function returns, all of its return values are returned as resume return values. So the resume execution succeeds here, and returns to "end".
Fourth time:
Copy Code code as follows:
Print ("Main", Coroutine.resume (Co, "X", "Y"))
Because the CO function has returned, it is in a dead state and cannot be resume, so the 4th time resume fails.