最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Lua協(xié)同程序函數(shù)coroutine使用實(shí)例

 更新時(shí)間:2014年09月23日 10:54:42   作者:iCode0410  
這篇文章主要介紹了Lua協(xié)同程序函數(shù)coroutine使用實(shí)例,協(xié)程是協(xié)同程序的簡(jiǎn)稱,顧名思義,就是協(xié)同工作的程序,需要的朋友可以參考下

協(xié)程是協(xié)同程序的簡(jiǎn)稱,顧名思義,就是協(xié)同工作的程序。協(xié)程擁有自己獨(dú)立的桟、局部變量和PC計(jì)數(shù)器,同時(shí)又與其他協(xié)同程序共享全局變量和其他大部分東西;

協(xié)程與線程的主要區(qū)別在于,一個(gè)多線程程序可以同時(shí)運(yùn)行幾個(gè)線程(并發(fā)執(zhí)行、搶占),而協(xié)同程序卻需要彼此協(xié)作地運(yùn)行,即一個(gè)多協(xié)程程序在任意時(shí)刻只能運(yùn)行一個(gè)協(xié)程,并且正在執(zhí)行的協(xié)程只會(huì)在其顯式地要求掛起(suspend)時(shí),它的執(zhí)行才會(huì)暫停(無搶占、無并發(fā))。

 Lua中所有與協(xié)程相關(guān)的函數(shù)都在coroutine(一個(gè)table)中; 函數(shù)create用于創(chuàng)建新的協(xié)程,只有一個(gè)參數(shù)——要執(zhí)行的函數(shù),返回一個(gè)thread類型的值。

thread的狀態(tài):suspend、running、dead、normal,可以通過coroutine.status(co)來檢查co的狀態(tài)。

創(chuàng)建一個(gè)thread時(shí),它處于掛起狀態(tài)。coroutine.resume函數(shù)用于啟動(dòng)或再次啟動(dòng)一個(gè)協(xié)程的執(zhí)行,并可以向coroutine傳遞參數(shù)。當(dāng)一個(gè)協(xié)程結(jié)束時(shí),主函數(shù)返回的值將作為resume的返回值。

coroutine.yield用于一個(gè)運(yùn)行中的協(xié)程掛起(suspend),之后可以再恢復(fù)(resume)。yield的返回值就是resume傳入的參數(shù)。

Lua的協(xié)程模型可以類比Python的generator。

一個(gè)簡(jiǎn)單的示例:

復(fù)制代碼 代碼如下:

> co = coroutine.create(function(a) while a > 0 do print(coroutine.yield(a)); a = a - 1; end return -1 end)
> return coroutine.resume(co, 3) --- 3是傳遞給主函數(shù)的
true        3
> return coroutine.resume(co, 4)
4
true        2
> return coroutine.resume(co, 5)
5
true        1
> return coroutine.resume(co, 6)
6
true        -1 ---主函數(shù)已經(jīng)返回
> return coroutine.resume(co, 7)
false        cannot resume dead coroutine
>

協(xié)程的應(yīng)用 —— 生產(chǎn)者/消費(fèi)者

需求:輸入一行,打印一行

復(fù)制代碼 代碼如下:

function send(x)
coroutine.yield(x)
end
 
function receive(co)
local s, v = coroutine.resume(co)
return v
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()
for line = 1, math.huge do
local x = receive(prod)
x = string.format('%5d %s', line, x)
send(x)
end
end)
end
 
function consumer(prod)
while true do
local x = receive(prod)
io.write(x, '\n')
end
end
 
prod = producer()
fil = filter(prod)
con = consumer(fil)

協(xié)程的應(yīng)用 —— 迭代器(類比Python Generator)
復(fù)制代碼 代碼如下:

function seq_generator(n)
local i = 1
while i <= n do
coroutine.yield(i)
i = i + 1
end
return nil
end
 
function seq(n)
local co = coroutine.create(function() seq_generator(n) end)
return function()
local s,v = coroutine.resume(co)
return v
end
end
 
for i in seq(4) do
print(i)
end

執(zhí)行
復(fù)制代碼 代碼如下:

lua seq_generator.lua
1
2
3
4

相關(guān)文章

最新評(píng)論

二连浩特市| 铁岭市| 南京市| 民乐县| 卓尼县| 米泉市| 城固县| 兴安县| 禹城市| 霍邱县| 武隆县| 鸡泽县| 丘北县| 沅陵县| 霍林郭勒市| 班玛县| 定襄县| 博罗县| 根河市| 蒙山县| 榆中县| 西畴县| 合阳县| 芜湖县| 丁青县| 老河口市| 龙陵县| 莲花县| 临潭县| 丁青县| 安宁市| 高碑店市| 容城县| 武夷山市| 万州区| 桂东县| 马尔康县| 古交市| 扶风县| 光山县| 修文县|