f24.1 生产者设计驱动模式
function producer()
while true do
local x = io.read()
send(x)
end
end
function consumer(x)
print(x)
while true do
print(receive())
end
end
function send(x)
coroutine.resume(consumer,x)
end
function receive()
return coroutine.yield()
end
consumer = coroutine.create(consumer)
producer()
本文介绍了一种使用Lua协程实现的生产者消费者模式。通过定义producer和consumer函数,利用coroutine的resume和yield方法进行消息传递,实现了生产者发送数据,消费者接收并打印数据的过程。

被折叠的 条评论
为什么被折叠?



