请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Lua5.3协程简单示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

来源 http://blog.csdn.net/vermilliontear/article/details/50547852

 

生产者->过滤器->消费者 模式的协程示例

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


--[[ "producer()" 创建了一个"coroutine", 由filter掌控; 
      "filter()" 创建了一个"coroutine", 由consumer掌控. --]]
consumer(filter(producer()))

 

运行截图现象

 

 

 coroutine.wrap 与 coroutine.create 的区别 

-- coroutine.wrap 返回函数
co1 = coroutine.wrap(function (a)
        local c = coroutine.yield(a+1)
        print("wrap yield before a and c: ", a, c)
        return 2 * a
    end)

b = co1(20)
print(b)
d = co1(20+30)
print(d)

print("----------------")

-- coroutine.create 返回协程本身
co2 = coroutine.create(function (a)
        local c = coroutine.yield(a+1)
        print("wrap yield before a and c: ", a, c)
        return 2 * a
    end)

k, v = coroutine.resume(co2, 20)
print(k, v)
k, v = coroutine.resume(co2, 20+30)
print(k, v)

 

运行现象:

 

 

使用”coroutines”实现了简单的抢占式线程

threads = {}
time = os.clock()
limit_time = 0.111

function cal(from, to)
    local sum = 0;
    for i = from, to, 1 do
        sum = sum + i
        if(os.clock() - time) >= limit_time then
            print(string.format("Worker %d calculating, limit_time(%f), time(%f), %f%%.", 
                worker, limit_time, time, (i / to * 100)))
            time = os.clock()
            coroutine.yield()
        end
    end
end

function job(from, to)
    local co = coroutine.create(function ()
            cal(from, to)
        end)
    table.insert(threads, co)
end

job(1, 10000)
job(1000, 50000)
job(5000, 60000)
job(10000, 70000)

while true do
    local n = #threads
    if n == 0 then
        break
    end
    for i = 1, n do
        worker = i -- 全局变量
        local status = coroutine.resume(threads[i])
        if not status then
            table.remove(threads, i)
            break
        end
    end
end

 

 

运行现象:

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
怎样将lua移植到arm平台的linux内核发布时间:2022-07-22
下一篇:
openresty+lua在反向代理服务中的玩法发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap