Examples of collaborative programming in Lua

Source: Internet
Author: User

Examples of collaborative programming in Lua

This article mainly introduces the collaborative programming in Lua, which is the basic knowledge in Lua's getting started learning. For more information, see

A synergistic program is a collaborative nature. Two or more methods can be executed in a controllable manner. With the help of a coprocessor, only one of its coprocessor programs can run at any given time. This requires that the coprocessor be paused only when it is running.

The above definition may seem vague. To tell it more clearly, suppose we have two methods, one main program method and the same program. When the restoration function is used to call the coroutine, it starts to execute. When the yield function is called, the execution is paused. Again, the same process can continue to call and execute another recovery function that has been paused. This process can continue until the end of the coroutine is executed.

Functions available for the coprocessor

The following table lists all available features used in the Lua collaboration program.

Example

Let's take an example to understand the concept of coroutine.

The Code is 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 will get the following output.

The Code is as follows:

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

What is the above example?

As mentioned earlier, we start with the restoration function and generate a function to stop the operation. In addition, you can see that the coroutine recovery function receives multiple return values. Every step of the above program will be explained here to make it clear.

First, we created a collaboration program and assigned it to the variable name collaboration and collaboration program. We need to use two variables as parameters.

When we call it the first recovery function, values 3 and 2 are kept in the temporary variables value1 and value2 until the end of the coroutine.

To understand this, we have used tempvar3 to initialize to 10, which is updated to 13 and 16 by subsequent calls of the coroutine, because value 1 is reserved as 3, the execution of the entire collaborative program.

The first coroutine. yield returns two values, 4 and 3, which are the recovery functions obtained by updating input parameters 3 and 2. It also receives the real/false status of coroutine execution.

Another thing about coroutine is how to restore the care written by calling the next parameter. In the example above, we can see that coroutine. yield assigns a variable to receive the next call parameter, which provides a powerful way to do the relationship between new businesses and existing parameter values.

Finally, when all the statements of the coprocessor are executed, false is returned for subsequent calls, and the statement "cannot restore the coprocessor" is used as a response.

Example of another coroutine

Let's look at a simple coprocessor that returns a number, from 1 to 5 yield function restoration. It creates a synergy program and restores the existing coroutine if it does not exist.

The Code is 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, 10 do

Print (index, getNumber ())

End

When we run the above program, we will get the following output.

The Code is as follows:

1 1

2 2

3 3

4

5 5

6 1

7 2

8 3

9 4

10 5

There are usually threads in the Coordination Program and multi-program language comparison, but you must understand that the Coordination Program thread has similar functions, but only one execution will not execute concurrently.

We control the execution sequence of programs to meet and provide the need to temporarily retain some information. The use of global variables and coroutine provides more flexible collaboration programs.

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.