欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Lua之协同程序coroutine代码实例

程序员文章站 2022-04-09 22:57:28
do --create coroutine table --coroutine state: suspended, running, dead, norma...
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