标签:
producer =coroutine.create(function () --生产者 while true do local x = io.read() --生产新的值 send(x) --发送给消费者 end end) function consumer() --消费者 while true do local x = receive() --从生产者接受值 io.write(x, "\n") --消费新的值 end end function receive() local status, value = coroutine.resume(producer) return value end function send(x) --发送 coroutine.yield(x) end consumer()
---------------------------------------------------------------------------------
function producer() --生产者
return coroutine.create(function()
while true do
local x = io.read() --生产新的值
send(x) --发送给消费者
end
end)
end
function receive(prod)
local status, value = coroutine.resume(prod)
return value
end
function send(x) --发送
coroutine.yield(x)
end
function filter(prod)
return coroutine.create(function ()
for line = 1, math.huge do
local x= receive(prod) --获取新值
x = string.format("%d %s", line, x)
send(x)
end
end)
end
function consumer(prod) --消费者
while true do
local x = receive(prod) --从生产者接受值
io.write(x, "\n") --消费新的值
end
end
p = producer()
f = filter(p)
consumer(f)
标签:
原文地址:http://www.cnblogs.com/vagaband/p/4242466.html