舉例詳解Lua中的協(xié)同程序編程
協(xié)同程序是協(xié)同的性質(zhì),可以把兩個或更多的方法以可控制的方式執(zhí)行。隨著協(xié)同程序,在任何給定的時間,只有其協(xié)同程序運行之一,這在運行協(xié)同程序只能暫停其執(zhí)行時,明確要求暫停。
上述定義可能看起來模糊。來告訴它更清楚,假設我們有兩個方法,一個主程序方法和協(xié)同程序。當我們使用恢復功能調(diào)用協(xié)程,其開始執(zhí)行,當我們調(diào)用yield功能,暫停執(zhí)行。再次同協(xié)程可以繼續(xù)從它被暫停的另一個恢復功能調(diào)用執(zhí)行。這個過程可以繼續(xù),直到執(zhí)行了協(xié)程的結(jié)束。
協(xié)同程序可用的功能
下表列出了在Lua協(xié)同程序及其相應的使用所有的可用功能。
例子
讓我們看一個例子就明白了協(xié)程的概念。
local tempvar3 =10
print("coroutine section 1", value1, value2, tempvar3)
local tempvar1 = coroutine.yield(value1+1,value2+1)
tempvar3 = tempvar3 + value1
print("coroutine section 2",tempvar1 ,tempvar2, tempvar3)
local tempvar1, tempvar2= coroutine.yield(value1+value2, value1-value2)
tempvar3 = tempvar3 + value1
print("coroutine section 3",tempvar1,tempvar2, tempvar3)
return value2, "end"
end)
print("main", coroutine.resume(co, 3, 2))
print("main", coroutine.resume(co, 12,14))
print("main", coroutine.resume(co, 5, 6))
print("main", coroutine.resume(co, 10, 20))
當我們運行上面的程序,會得到下面的輸出。
main true 4 3
coroutine section 2 12 nil 13
main true 5 1
coroutine section 3 5 6 16
main true 2 end
main false cannot resume dead coroutine
上面的例子是做什么?
如之前所提到的,我們使用恢復功能的動作開始,并產(chǎn)生函數(shù)來停止操作。此外,可以看到有由協(xié)程恢復功能接收多個返回值。這里將解釋上面的程序每一個步驟,使之清楚。
- 首先,我們創(chuàng)建了一個協(xié)同程序,并把它分配給變量名合作和協(xié)同程序需要在兩個變量作為參數(shù)。
- 當我們稱之為第一恢復功能,值3和2分別被保持在臨時變量value1和value2,直到協(xié)程的結(jié)束。
- 為了理解這一點,我們已經(jīng)使用了tempvar3初始化為10,它被由協(xié)程的后續(xù)調(diào)用更新為13和16,因為值1被保留為3,整個協(xié)同程序的執(zhí)行。
- 第一個coroutine.yield返回兩個值4和3 是由更新輸入?yún)?shù)3和2,yield語句得到了恢復函數(shù)。它還接收協(xié)程執(zhí)行的真/假狀態(tài)。
- 關(guān)于協(xié)程的另一件事是如何恢復調(diào)用下一參數(shù)寫成的照顧,在上述的例子; 可以看到,coroutine.yield分配變量接收到下一次調(diào)用參數(shù),它提供做新業(yè)務與現(xiàn)有參數(shù)值之間的關(guān)系的一種強有力的方式。
- 最后,一旦在協(xié)同程序的所有語句執(zhí)行時,后續(xù)調(diào)用將返回false,并且“不能恢復死協(xié)同程序”語句作為回應。
另一個協(xié)程的例子
讓我們來看一個簡單的協(xié)同程序返回一個數(shù)字,從1到5 yield函數(shù)恢復功能。它創(chuàng)建協(xié)同程序,如果沒有則恢復現(xiàn)有的協(xié)程。
local function getNumberHelper()
co = coroutine.create(function ()
coroutine.yield(1)
coroutine.yield(2)
coroutine.yield(3)
coroutine.yield(4)
coroutine.yield(5)
end)
return co
end
if(numberHelper) then
status, number = coroutine.resume(numberHelper);
if coroutine.status(numberHelper) == "dead" then
numberHelper = getNumberHelper()
status, number = coroutine.resume(numberHelper);
end
return number
else
numberHelper = getNumberHelper()
status, number = coroutine.resume(numberHelper);
return number
end
end
for index = 1, 10 do
print(index, getNumber())
end
當我們運行上面的程序,會得到下面的輸出。
2 2
3 3
4 4
5 5
6 1
7 2
8 3
9 4
10 5
往往有協(xié)同程序與多道程序語言的線程的比較,但要明白,協(xié)同程序線程有類似的功能,但只有一次執(zhí)行,并不會執(zhí)行兼任。
我們控制程序的執(zhí)行順序,以滿足與提供暫時保留某些信息的需求。使用全局變量與協(xié)程,提供了協(xié)同程序更加靈活。
相關(guān)文章
lua讀取redis數(shù)據(jù)的null判斷示例代碼
最近在工作中遇到了一個問題,通過查找相關(guān)資料才得知原因是因為返回結(jié)果的問題,下面這篇文章主要給大家介紹了關(guān)于lua讀取redis數(shù)據(jù)的null判斷的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2018-09-09Lua中的元表(metatable)、元方法(metamethod)詳解
這篇文章主要介紹了Lua中的元表(metatable)、元方法(metamethod)詳解,本文對它做了詳細講解,并給出實例來說明,需要的朋友可以參考下2014-09-09