A detailed example of collaborative programming _lua in Lua

Source: Internet
Author: User
Tags lua

A collaborative program is a collaborative nature that can be performed in a controlled manner by two or more methods. With the synergistic program, at any given time, only its synergistic program runs one, which in the running of the synergistic program can only suspend its execution when explicitly asked to suspend.

The above definition may look blurry. To tell it more clearly, suppose we have two methods, a main program method and a synergistic program. When we use the Restore function to invoke the coprocessor, it starts execution, and when we call the yield function, suspend execution. The same coprocessor can continue to be invoked again from another recovery function that it was paused to perform. This process can continue until the end of the coprocessor is performed.
features available with the collaboration program

The following table lists all the available features in the LUA collaboration program and its corresponding use.

Example

Let's look at an example to understand the concept of the coprocessor.

Copy Code code as follows:
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))

When we run the above program, we get the output below.

Copy Code code as follows:
Coroutine Section 1 3 2 10
Main true 4 3
Coroutine Section 2 Nil 13
Main true 5 1
Coroutine Section 3 5 6 16
Main true 2 End
Main false cannot resume dead coroutine

What is the example above?

As mentioned earlier, we start with the action of the restore function and produce a function to stop the operation. In addition, you can see that there are multiple return values received by the covariant recovery feature. Here will explain each step of the above procedure to make it clear.

    • First, we create a collaborative program and assign it to variable name cooperation and collaboration programs need to be in two variables as parameters.
    • When we call the first restore function, values 3 and 2 are kept in the temporary variable value1 and value2 respectively until the end of the coprocessor.
    • To understand this, we have used TEMPVAR3 initialization to 10, which is updated by the subsequent calls of the coprocessor to 13 and 16, since the value 1 is reserved for 3 and the entire collaborative program executes.
    • The first Coroutine.yield returns two values 4 and 3 is the recovery function obtained by the update input parameters 3 and the 2,yield statement. It also receives the true/False state of the coprocessor execution.
    • Another thing about the coprocessor is how to restore the invocation of the next parameter written in the care, in the example above; As you can see, the Coroutine.yield assignment variable receives the next call parameter, which provides a powerful way to do the relationship between the new business and the existing parameter values.
    • Finally, once all the statements in the collaboration program are executed, the subsequent call returns false and the "Cannot recover deadlock" statement as a response.

Another example of a co-process

Let's look at a simple collaborative program that returns a number from 1 to 5 yield function recovery function. It creates a collaboration program, and if not, restores the existing coprocessor.

Copy Code code as follows:
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
Print (index, getnumber ())
End

When we run the above program, we get the output below.

Copy Code code as follows:
1 1
2 2
3 3
4 4
5 5
6 1
7 2
8 3
9 4
10 5

There is often a comparison between a collaborative program and a multiple-program language thread, but it is understood that a collaborative program thread has similar functionality but only one execution and does not perform concurrently.

Our control procedures are executed in order to meet the need to provide temporary retention of certain information. The use of global variables and the coprocessor provides more flexibility in the collaborative process.

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.