do
--create coroutine table
--coroutine state: suspended, running, dead, normal
--when create the coroutine, the status is suspended, after calling it, the status is dead
--get the coroutine status by the way coroutine.status
local coa = 0;
local cob = 0;
function createcoroutinea()
coa = coroutine.create(
function()
--for i = 0, 10 do
print("coa: ", 0);
print("cob status: ", coroutine.status(cob)); --normal status
print("coa status: ", coroutine.status(coa));
print("coa coroutine next status");
--coroutine.yield();--the current coroutine is suspended
--end
end
);
print("from coa to resume cob");
end
function createcoroutineb()
cob = coroutine.create(
function()
--for i = 0, 10 do
print("cob: ", 0);
print("coa status: ", coroutine.status(coa));
coroutine.resume(coa); --when resume coa, the cob will suspended, calling cob ,the coa status is
--suspended and dead, this time will continue to execute the next code
print("cob status: ", coroutine.status(cob));
print("cob coroutine next status");
--coroutine.yield();
--end
end
);
print("from cob to resume coa");
end
--display the coa and cob status
createcoroutinea();
print(coroutine.status(coa));
createcoroutineb();
print(coroutine.status(cob));
coroutine.resume(cob);
print(coroutine.resume(cob)); --if the coroutine is dead ,the resume will resume false, and can't resume the dead coroutine
--print("coa status: ", coroutine.status(coa));
--print("cob status: ", coroutine.status(cob));
end
注:
resume得到返回值,
如果有对应的yield在wait resume,那么yield的参数作为resum的返回值,第一个返回值表示coroutine没有错误,后面的返回值个数及其值视yeild参数而定。
如果没有yield在wait,那么返回值是对应函数的返回值,:true,* * *
do
--create coroutine table
--coroutine state: suspended, running, dead, normal
--when create the coroutine, the status is suspended, after calling it, the status is dead
--get the coroutine status by the way coroutine.status
local coa = 0;
local cob = 0;
function createcoroutinea()
coa = coroutine.create(
function(parama, paramb)
--for i = 0, 10 do
print("coa: ", 0);
coroutine.yield(parama, paramb);--the current coroutine is suspended
--end
return 100, 200;
end
);
print("from coa to resume cob");
end
function createcoroutineb()
cob = coroutine.create(
function()
--for i = 0, 10 do
print("cob: ", 0);
print("coa status: ", coroutine.status(coa));
coroutine.resume(coa); --when resume coa, the cob will suspended, calling cob ,the coa status is
--suspended and dead, this time will continue to execute the next code
print("cob status: ", coroutine.status(cob));
print("cob coroutine next status");
--coroutine.yield();
--end
end
);
print("from cob to resume coa");
end
createcoroutinea();
--if not yield is waiting ,the return values that the main function return as the results of the resume
--or the return as the yield params
print( coroutine.resume(coa, 10, 20));--output:true, 10, 20
end
段子就是本黄书