首页 > 代码库 > Lua chapter 8 协同程序
Lua chapter 8 协同程序
1、协同程序,一个具有多个协同程序的程序在任意时刻,只能运行一个协同程序,
只有正在运行的协同程序被挂起时,它的执行才会暂停。
创建
co = coroutine.create(匿名函数); -- 匿名函数就是线程要执行的东东
状态
coroutine.status(co);
唤醒
coroutine.resume(co);
挂起
coroutine.yield();
coroutine.yield(a*10,b*10);
return "success";
end;
co = coroutine.create(f);
print (coroutine.resume(co,1,2)); --> true 10,20
print (coroutine.resume(co,1,2)); --> true success
function receive()
local status, value = http://www.mamicode.com/coroutine.resume(producer);
return value;
end;
function send(value)
coroutine.yield(value);
end;
-- set random seed
math.randomseed(os.time());
producer = coroutine.create(
function()
while true do
value = http://www.mamicode.com/math.random(1000);
send(value);
end;
end
);
consumer = coroutine.create(
function()
while true do
print ("receive ? y or n");
if "y" == io.read() then
local value = http://www.mamicode.com/receive();
print("receive: " .. value .."\n");
else
break;
end;
end;
end
);
for i=1, 100 do
print("f1", i);
coroutine.yield();
end;
end;
function f2()
for i=1, 100 do
print(" f2", i);
coroutine.yield();
end;
end;
c1 = coroutine.create(f1);
c2 = coroutine.create(f2);
thread ={c1,c2};
function dispatch()
math.randomseed(os.time());
while true do
var = math.random(#thread); -- 随机数,决定执行哪个协同程序
local status = coroutine.resume(thread[var]);
if status==false then -- 如果一条协同程序执行完,则remove
print("thread dead");
table.remove(thread,var);
end;
if #thread == 0 then -- 协同程序执行完,退出循环
break;
end;
end;
end;
只有正在运行的协同程序被挂起时,它的执行才会暂停。
创建
co = coroutine.create(匿名函数); -- 匿名函数就是线程要执行的东东
状态
coroutine.status(co);
唤醒
coroutine.resume(co);
挂起
coroutine.yield();
2、yield-resume 数据交换
function f(a,b)coroutine.yield(a*10,b*10);
return "success";
end;
co = coroutine.create(f);
print (coroutine.resume(co,1,2)); --> true 10,20
print (coroutine.resume(co,1,2)); --> true success
3、消费者,生产者问题 producer and consumer
function receive()
local status, value = http://www.mamicode.com/coroutine.resume(producer);
return value;
end;
function send(value)
coroutine.yield(value);
end;
-- set random seed
math.randomseed(os.time());
producer = coroutine.create(
function()
while true do
value = http://www.mamicode.com/math.random(1000);
send(value);
end;
end
);
consumer = coroutine.create(
function()
while true do
print ("receive ? y or n");
if "y" == io.read() then
local value = http://www.mamicode.com/receive();
print("receive: " .. value .."\n");
else
break;
end;
end;
end
);
coroutine.resume(consumer);
4、Lua的协同程序,在任意时刻只能有一个在执行,通过yield-resume来实现不同协同程序间的切换
function f1()for i=1, 100 do
print("f1", i);
coroutine.yield();
end;
end;
function f2()
for i=1, 100 do
print(" f2", i);
coroutine.yield();
end;
end;
c1 = coroutine.create(f1);
c2 = coroutine.create(f2);
thread ={c1,c2};
function dispatch()
math.randomseed(os.time());
while true do
var = math.random(#thread); -- 随机数,决定执行哪个协同程序
local status = coroutine.resume(thread[var]);
if status==false then -- 如果一条协同程序执行完,则remove
print("thread dead");
table.remove(thread,var);
end;
if #thread == 0 then -- 协同程序执行完,退出循环
break;
end;
end;
end;
dispatch();
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。