Lua 5.3 協程簡單示例

Lua 5.3 協程簡單示例

來源 http://blog.csdn.net/vermilliontear/article/details/50547852函數

 

生產者->過濾器->消費者 模式的協程示例spa

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()))

 

運行截圖現象.net

 

 

 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)

 

運行現象:code

 

 

使用」coroutines」實現了簡單的搶佔式線程orm

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

 

 

運行現象:協程

相關文章
相關標籤/搜索