The LUA coprocessor (coroutine) called 4 resume Analysis

Source: Internet
Author: User
Tags lua

This is a section of the code that analyzes the LUA coprocessor (collaborative program, Coroutine):

function foo (a)
print ("foo", a) return
Coroutine.yield (2*a)
end
CO = coroutine.create (function , b)
print ("Co-body1", A, b)
r = foo (a+1)
print ("Co-body2", R) Local
r, s = Coroutine.yie LD (A+B, a-b)
print ("Co-body3", R, s) return
B, "end" end
) print ("
1----")
print ("main", Corou Tine.resume (Co, 1,) 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:

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:

Print ("Main", Coroutine.resume (Co, 1, 10))

Execute print ("Co-body1", A, b), the values of A and B are provided for resume, a=1, b=10;
Compute the a+1=2, enter Foo (a), and pass the result of the calculation just through a parameter, execute print ("foo", a);
Consider return Coroutine.yield (2*a);
Calculate 2*a=4, hit yield, suspend foo (a) call, return 4 to resume. Note that Foo's return has not been implemented;
Resume execution succeeded, returns True, 4.

Second time:

Print ("Main", Coroutine.resume (CO, "R"))

Executes from the last pending foo (a) call, followed by a return call that is not completed;
The value returned by foo (a+1) is the string "R" because yield returns the call parameter of 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.
Execute print ("Co-body2", R), and R has a value of "R";
Consider local r, s = Coroutine.yield (a+b, a-b);
Calculates a+b=11, a-b=-9, encounters yield, hangs the call of CO, returns 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.
Resume execution succeeded, returns True, 11,-9.

Third time:

Print ("Main", Coroutine.resume (Co, "X", "Y"))

Start execution from the last yield, and then perform a local r, S = assignment that is not completed. As mentioned above, yield returns the invocation parameters of resume, so the value of R and S is "X" and "Y";
Execute print ("Co-body3", R, s) for printing;
Consider return B, "end";
B's value is always 10 unchanged, here directly returned, and also return the "End" of this string;
All of its return values are returned as resume return values as the covariant function returns. So the resume execution succeeds here, and returns to "end".

Fourth time:

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.