A synergistic program is about the same as threading thread, which is an execution sequence that has its own stack, local variables, and command pointers, while sharing global variables and most other things with other co-programs. Conceptually, the main difference between a thread and a co-worker is that a program with multiple threads can run several threads at the same time, while a co-program needs to work cooperatively. This means that multiple co-programs can only run one co-program at any time, and its execution will only be paused if the runtime's explicit request hangs.
co-Program coroutine lua All the functions about the synergistic program are placed in a table called "Coroutine". 1, Coroutine.create creates a value of type thread that represents a new synergistic program, and returns a synergistic program.  2, coroutine.status Check the status of the synergistic program (suspend suspended, run running, death dead, regular normal). 3, Coroutine.resume start or start a synergistic program again and change its status from suspended to run.  4, Coroutine.yield let a collaborative program hang. 5, Coroutine.wrap also creates a new synergistic program that returns a function. Creating a synergistic program: the CREATE function, which takes a function value as the execution content of a synergistic program, and returns a synergistic program. Start or restart a synergistic program: the Resume function, which accepts a synergistic program and one or more parameters for the value to be passed to the co-program. Suspend co-operation: yield function, let a cooperative program hang, and wait for the next time to resume its run. It can accept all parameters passed in by the resume function. With regard to the use of the Wrap function, the Wrap function is easier to use than the CREATE function. It provides a function that is actually required for the programming of a synergistic program, a function that wakes up a synergistic program. But there is also a lack of flexibility. You cannot check the status of the synergistic program created by wrap, and you cannot detect runtime errors. Various collaboration demonstrations: First, LUA provides a kind of "asymmetric collaboration program." In other words, LUA provides two functions to control the execution of a synergistic program, one for suspending execution and the other for resuming execution. Some other languages provide a "symmetric synergy" in which only one function is used to transfer execution between the cooperating programs. Second, tubeTao and filter filter Examples of collaborative programs are "producer-consumer" issues. It involves two functions, one function continuously produces values, and the other function consumes these values continuously. When the consumer needs a new value, it wakes the producer. The producer stops running after returning a new value, waiting for the consumer to wake up again. This design is called "consumer-driven". It is easy to implement a program by exchanging values between Resume-yield functions. filter, a processing function that is located between the producer and the consumer, allows for data conversion. It is both a consumer and a producer, which awakens the producer to produce new values and then passes the transformed values to the consumer.
1 --Filter filter for pipes and filters2 --The producer and the consumer pass the value through the filter3 --this model is generated by consumer-driven producers. 4 5 --counter Functions6 functionGetCount (x)7 return function()8x=x+19 returnxTen End One End A --create a closed counter - LocalCount = GetCount (0) - --Send new Value the functionSend (x) - Coroutine.yield(x) - End - --Start a synergistic program + functionreceive (PRO) - LocalStatus,value =Coroutine.resume(PRO) + returnvalue A End at --producers - functionProducter () - while true Do - Send (count ()) - End - End in --filter, accept a producer - functionfilter (PRO) to Localx =0 + return function() - while true Do thex =receive (PRO) * Send (x) $ EndPanax Notoginseng End - End the --consumers, accept a producer's collaborative process and control conditions to prevent the cycle of death + --Suppose there are 100 consumers that drive producers to produce A functionConsumer (pro,num) the Localx =0 + whilex < num Do -x =receive (PRO) $ Print(x) $ End - End - the LocalPro =coroutine.create(Producter) - LocalFil =coroutine.create(Filter (PRO))WuyiConsumer (fil, - ) the - Print("Consumer Synergy program Status:",Coroutine.status(PRO)) Wu Print("Producer Synergy Program Status:",Coroutine.status(fil))
Lua Learning Notes (ix) Synergy program (Threads thread)