在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
lua的corroutine学习 function receive (prod) local status, value = coroutine.resume(prod) return value end function send (x) coroutine.yield(x) end function producer () return coroutine.create( function () while true do local x = io.read() send(x) end end) end function filter (prod) return coroutine.create( function () local line = 1 while true do local x = receive(prod) x = string.format("%5d %s", line, x) send(x) line = line + 1 end end) end function consumer (prod) while true do local x = receive(prod) io.write(x, "\n") end end p = producer() -->creates a coroutine p, which yield after read a line from stdio agin and agin f = filter(p) -->creates a coroutine f, which resume the p at first and yeild after the string processed consumer(f) -->resumes the coroutine f, and writes the string f processed
|
请发表评论