[Lua notes], Lua coroutine

Lua coroutines are similar to threads: they have independent stacks, independent local variables, and independent instruction pointers, while sharing global variables and most other things with other coroutines.

The main difference between threads and coroutines is that a program with multiple threads can run several threads at the same time, while coroutines need to cooperate with each other to run.
Only one coroutine is running at any given time, and the running coroutine is suspended only when explicitly asked to.
Coroutines are somewhat similar to synchronized multithreading, and several threads waiting for the same thread lock are somewhat similar to coordination.

create

Create a coroutine, return the coroutine, the parameter is a function, and wake up the function call when used in conjunction with resume

resume

Restart coroutine, use with create

wrap

Create a coroutine and return a function. Once you call this function, you will enter the coroutine. The difference from create is that you don't need to use resume to wake up.

yield

1. Set the coroutine to a suspended state, and wait for the resume to trigger an event again.
2. Yield processes the parameters of the function and returns it to resume.
3. Define the parameters that need to be passed when the next wake-up occurs.
4. The next execution of the coroutine starts from the place where the yield is suspended, and the parameters of the resume are passed into the yield and assigned to the function for use.

status

Check the state of coroutine Note: There are three states of coroutine: dead, suspended, running

running

Return the running coroutine, a coroutine is a thread, when using running, it returns a corouting thread number

Example:

local function func(a)
	print("第一次协同程序执行输出", a) -- 第一次传的参数 1
    local a = coroutine.yield(a+10,"返回主线程") -- 返回11 -- 第二次调用resume的参数2赋值给a
		
    print("第二次协同程序执行输出", a) --2
    local a = coroutine.yield(a+100,"返回主线程")  -- 返回102  -- 第二次调用resume的参数3赋值给a
     
    print("第三次协同程序执行输出", a) -- 3
    return a, "结束协同程序"          --最后一次赋值的a
end

co = coroutine.create(func,a)
       
print("main", coroutine.resume(co, 1)) -- true, 11
print("------------------------------")
print("main", coroutine.resume(co, 2)) -- true 102	
print("------------------------------")
print("main", coroutine.resume(co, 3)) -- true 3
print("------------------------------")
print("main", coroutine.resume(co, 4)) -- cannot resume dead coroutine
print("------------------------------")

Call resume to wake up the coroutine successfully and return true.
Run the function (func) in the coroutine (co), and start executing the other parameters of resume as the parameters of the function (func), until the coroutine (co) is suspended when yield is encountered.
Return at yield, and return to the main thread after processing the parameters of yield.
The second call to resume starts execution at the place where the last suspension (yield) was, and the other parameters passed in are used as the return value of this yield.
And so on until the function is executed.

The strength of the cooperation between resume and yield is that resume is in the main process, and it passes the external state (data) into the coroutine; while yield returns the internal state (data) to the main process.

Guess you like

Origin blog.csdn.net/qq_33461689/article/details/127075382