協(xié)同程序是協(xié)同的性質(zhì),可以把兩個或更多的方法以可控制的方式執(zhí)行。隨著協(xié)同程序,在任何給定的時間,只有其協(xié)同程序運行之一,這在運行協(xié)同程序只能暫停其執(zhí)行時,明確要求暫停。
上述定義可能看起來模糊。來告訴它更清楚,假設(shè)我們有兩個方法,一個主程序方法和協(xié)同程序。當(dāng)我們使用恢復(fù)功能調(diào)用協(xié)程,其開始執(zhí)行,當(dāng)我們調(diào)用yield功能,暫停執(zhí)行。再次同協(xié)程可以繼續(xù)從它被暫停的另一個恢復(fù)功能調(diào)用執(zhí)行。這個過程可以繼續(xù),直到執(zhí)行了協(xié)程的結(jié)束。
下表列出了在Lua協(xié)同程序及其相應(yīng)的使用所有的可用功能。
| S.N. | Method & Purpose |
|---|---|
| 1. |
coroutine.create (f): 創(chuàng)建一個新的協(xié)程與函數(shù)f 和 返回類型“線程”的對象。 |
| 2. |
coroutine.resume (co [, val1, ...]): 重新開始corountine co和傳遞的參數(shù)(若有)。它返回操作和可選的其他返回值的狀態(tài)。 |
| 3. |
coroutine.running (): 返回運行的協(xié)同程序或零,如果調(diào)用主線程。 |
| 4. |
coroutine.status (co): 返回運行,正常其中一個值,暫?;蛩劳龅幕A(chǔ)上,協(xié)程的狀態(tài)。 |
| 5. |
coroutine.wrap (f): 像coroutine.create,coroutine.wrap函數(shù)還創(chuàng)建了一個協(xié)同程序,但不是返回協(xié)同程序本身,它返回一個函數(shù)被調(diào)用時,將恢復(fù)協(xié)程。 |
| 6. |
coroutine.yield (...): 暫停運行協(xié)程。傳遞給該方法的參數(shù)作為附加的返回值以恢復(fù)功能。 |
讓我們看一個例子就明白了協(xié)程的概念。
co = coroutine.create(function (value1,value2) 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))
當(dāng)我們運行上面的程序,會得到下面的輸出。
coroutine section 1 3 2 10 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
如之前所提到的,我們使用恢復(fù)功能的動作開始,并產(chǎn)生函數(shù)來停止操作。此外,可以看到有由協(xié)程恢復(fù)功能接收多個返回值。這里將解釋上面的程序每一個步驟,使之清楚。
首先,我們創(chuàng)建了一個協(xié)同程序,并把它分配給變量名合作和協(xié)同程序需要在兩個變量作為參數(shù)。
當(dāng)我們稱之為第一恢復(fù)功能,值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語句得到了恢復(fù)函數(shù)。它還接收協(xié)程執(zhí)行的真/假狀態(tài)。
關(guān)于協(xié)程的另一件事是如何恢復(fù)調(diào)用下一參數(shù)寫成的照顧,在上述的例子; 可以看到,coroutine.yield分配變量接收到下一次調(diào)用參數(shù),它提供做新業(yè)務(wù)與現(xiàn)有參數(shù)值之間的關(guān)系的一種強有力的方式。
最后,一旦在協(xié)同程序的所有語句執(zhí)行時,后續(xù)調(diào)用將返回false,并且“不能恢復(fù)死協(xié)同程序”語句作為回應(yīng)。
讓我們來看一個簡單的協(xié)同程序返回一個數(shù)字,從1到5 yield函數(shù)恢復(fù)功能。它創(chuàng)建協(xié)同程序,如果沒有則恢復(fù)現(xiàn)有的協(xié)程。
function getNumber() 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
當(dāng)我們運行上面的程序,會得到下面的輸出。
1 1 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é)同程序更加靈活。