LUA Learning Notes-Chapter 9.1-Collaborative programs

Source: Internet
Author: User
Tags lua thread

1. What is a collaborative program

A synergistic program (Coroutine) is similar to a thread: it has a separate stack, independent local variables, independent instruction pointers, but a lot of information about sharing global variables with other co-programs.

The main difference between a thread and a synergistic program is that, in a multiprocessor case, a multithreaded program is conceptually running multiple threads concurrently, and the collaboration is done by collaborating, where only one co-program is running at any given moment, and the running collaborator is suspended only when it is explicitly requested to hang. A collaborator is a bit like a synchronized multi-threading, and several threads waiting for the same thread lock are somewhat similar in synergy.
The difference between synergy and sequential execution. At any given time, only one cooperative program can be run, and this differs from the sequential execution. The key is the yield function. The program will not be able to move forward in this place if the CPU time is spent in sequential execution or if a resource has been waiting. The appearance of the synergistic program is that the thread waiting for the resource can make up the resources and perform the next operation of the cooperative program. Yield can be suspended in the execution of an error, and then the next time the recovery is done

2, the cooperation (Coroutine) several states. Suspend, running (run), dead (the state after the function is gone, this time can no longer resume). Use the function status to view the status of the Coroutine.

Suspend state: When a collaboration has just been created or after a call to Coroutine.yield.
Run state: The function Coroutine.resume can make the program from the suspended state to the operating state
Stop state: The end of the cooperative program, enter the stop state

Normal: The state between suspend and run. Follow up with additional notes.

Example 1:

CO = coroutine.create (function () print (' Hi ') end)
print (Co,type (CO))
print (Coroutine.status (CO))--Check the status of the collaboration
Coroutine.resume (CO)--Changes the program from a pending state to a running
print (Coroutine.status (CO))--Prints the hi and enters the dead state

Coroutine seems to be just as well, like function calls, just more complex function calls. However, the real strength of coroutine is its yield function, which suspends the running coroutine and can be woken up again at the appropriate time and then continues to run.

Example 2: Using the yield function to suspend the running code

CO = coroutine.create (function () 
	for i=1,4 do
	print ("Co", I)
	Coroutine.yield ()--run code hangs
	end
	end)
Coroutine.resume (CO)--"Co 1 print" (
Coroutine.status (CO))-->suspended
--Suspends the program with the yield function, When the suspended program is activated, yield returns and continues until the program runs until the yield is encountered or the program ends
Coroutine.resume (CO)-->co 2, activating the program and continuing to run
Coroutine.resume (CO)-->co 3, continue running. Because in the programming we can see that the program is suspended in each loop.
Coroutine.resume (CO)
Coroutine.resume (CO)--print nothing. This is because, at the last call, the co-body has ended and the co-program is in the terminating state. If you still attempt to activate TA at this point, resume will return false and error message
print (Coroutine.resume (CO))

The coroutine is suspended every time a line is printed, when the yield function is run. When we wake the coroutine with a resume, the coroutine continues to run and prints the next line. The coroutine exits the loop and changes to the dead State (note the last state changes) until nothing is printed at the end. If a resume is performed on a coroutine of a dead state, then resume returns FALSE+ERR_MSG,

Note: The resume runs in protected mode, so if there is an error in the orchestration, Lua does not throw an error but returns the error to the resume function. Therefore, you can see the specific error by printing the resume's return information.

When a coroutine A is in the resume of another coroutine B, the state of a is not changed to suspended, we cannot resume it, but it is also not running state, because B is currently being running. At this point A's state is actually the normal state.

3, a pair of Resume-yield in Lua can exchange data with each other.

1) There is no yield in the main function, when calling resume, the extra arguments are passed to the main function of coroutine as the argument, the following example, 1 2 3 is the value of a B C, respectively:

CO = coroutine.create (function (a,b,c)
print ("Co", a,b,c+2)
--coroutine.yield (a+b,a-b)
end)
Coro Utine.resume (co,1,2,3)--co 1 2 5
print (Coroutine.resume (co,1,2,3))--false cannot resume dead coroutine

2) There is yield in the main function, and all arguments passed to yield are returned. Therefore, the return value of the resume, in addition to true for the flag to run correctly, also has a parameter value passed to yield:
CO = coroutine.create (function (a,b,c)
	Coroutine.yield (a+b,a-b)
	end)
--coroutine.resume (co,1,2,3)
Print (Coroutine.resume (co,1,2,3))-->true	3	-1

If it is called first time.

CO = coroutine.create (function (a,b,c)
	Coroutine.yield (a+b,a-b)
	end)
coroutine.resume (co,1,2,3)-- The return result is not printed, but the return result at this time is the true	3	-1
print (Coroutine.resume (co,1,2,3))-->true, at which time the return result is only one true

3) Yield also returns the extra parameters to the corresponding resume, as follows

CO = coroutine.create (function (b)
	print ("Co1", a) print ("
	CO2", Coroutine.yield ())
	print ("Co3", Coroutine.yield ())
	print ("Co4", Coroutine.yield ())
	end)
Coroutine.resume (Co, ' Hello ', 2,3,4)
Coroutine.resume (Co, ' Hello ', 2,3,4)--The first resume does not have print CO2 because yield is not returned, and the corresponding print ("CO2", Coroutine.yield ()) is not running
Coroutine.resume (co,1,2,3,4,5)--yield return the extra parameters to resume
coroutine.resume (co,10.,11,12)
The results of the operation are as follows:

Note the correspondence between them. If print ("Co1", a) is changed to print ("Co1", Coroutine.yield ()).

The code is as follows:

CO = coroutine.create (function (b)
	print ("Co1", Coroutine.yield ())
	print ("CO2", Coroutine.yield ())
	Print ("Co3", Coroutine.yield ())
	print ("Co4", Coroutine.yield ())
	end)
Coroutine.resume (Co, ' he ', 2,3,4)
Coroutine.resume (Co, ' Hello ', 5,6,7)--
Coroutine.resume (co,1,2,3,4,5)--yield return redundant parameters to resume
Coroutine.resume (co,10.,11,12)
The results of the operation are as follows:


As can be seen from the above running results, the resume is called four times, but the result is only 3 times. The first call to Coroutine.resume (CO, ' he ', 2,3,4) did not return.

4) When a coroutine is finished, all return values of the main function are returned to the resume:

CO = coroutine.create (function ()
	return 6,7
	end)
print (Coroutine.resume (CO))-->true 6 7

LUA provides coroutine asymmetric, the so-called asymmetric coroutine, meaning that it requires a function (yield) to suspend a coroutine, But another function (resume) is required to wake up the suspended coroutine. Corresponding, some languages provide symmetric coroutine, which is used to toggle the current coroutine function only one.

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.