Error HandlingASSERT (exp) error ("error message text") Pcall secure call
Collaborative Programs LUA is not really multi-threaded, it is a multi-threaded LUA non-symmetric collaboration Program (Semi-coroutine) that uses a synergistic program, which provides two functions to handle execution at any one time only one cooperative program executes You can only hang yourself until you stop working, and you can't stop working from the outside. as the producer consumer model mutually exclusive synergy all the functions of the synergistic program are placed in the Coroutine table CO = coroutine.create ( function () print ("wangning") end) ASSERT (Type (CO) = = "Thread") Create parameter is what the program executes, returning a variable of type thread 4 statuses: Suspend (suspended), run (running), Death (dead), normal newly created co-program is pending, can check status assert with status ( Coroutine.status (CO) = = "suspended") Coroutine.resume (CO) is used to start or restart the co-program, changing the status to running When the co-program executes the completion task function in the dead state use Coroutine.yield () in the task function to suspend the cooperative program and then call Coroutine.resume () to start the suspended co-operation again If the co-program is already in the dead state, resume will return false failure when the co-program a wakes up another cooperative program B, a will stop execution and b execution then A's status is normal normal You can use resume and yield to exchange data the extra arguments for the first call to resume (the second one later), which will be treated as parameters of the co-program task function passed in will be the return value for yield The first return value of the resume indicates a successful failure, and the second value is the incoming parameter of yield The return value of the function will be the additional return value of the resume when the task function of the co-operation is completed CO = coroutine.create ( function (a,b,c) print (A,B,C) print (Coroutine.yield (4,5,6)) return 10 end) res, a,b,c = Corouti Ne.resume (co,1,2,3)--1 2 3 print (RES,A,B,C)--True 4 5 6 res, d = Coroutine.resume (Co, 7,8,9)--> ; 7 8 9 Print (res, D)--True 10 print (Coroutine.resume (CO))--false cannot resume dead coroutine can use a cooperative program responsible for the main task cycle, Judge other cooperative program state, have idle to make it execute, realize multithreading function
Lua Learning Notes (vii)