-------------------------------------------------------------------------------- -- 不攜帶參數 -------------------------------------------------------------------------------- local main = function() print("step1") coroutine.yield() print("step2") end local co = coroutine.create(main) coroutine.resume(co) coroutine.resume(co)
-- 輸出
step1
step2
以上是最簡單的協程演示了html
講解:多線程
1.coroutine.create建立一個協程函數
2.第一次coroutine.create運行到main函數的coroutine.yield()函數處中止this
3.再次運行coroutine.create而後繼續在main函數中斷處運行,即運行main函數的第二個printlua
協程的參數稍微麻煩一些,最好能講下面的詳解看完,就能明白了,這裏先上個最簡單的演示參數傳遞的例子spa
-------------------------------------------------------------------------------- -- 攜帶參數傳遞 -------------------------------------------------------------------------------- local main = function(arg1) print (arg1) print (coroutine.yield("yield")) print (arg1) end local co = coroutine.create(main) print(coroutine.resume(co, "resume1")) print(coroutine.resume(co, "resume2"))
-- ouput: resume1 true yield resume2 resume1 true
Lua 支持 coroutine ,這個東西也被稱爲協同式多線程 (collaborative multithreading) 。 Lua 爲每一個 coroutine 提供一個獨立的運行線路。 然而和多線程系統中的線程不一樣,coroutine 只在顯式的調用了 yield 函數時纔會掛起。線程
建立一個 coroutine 須要調用一次 coroutine.create 。 它只接收單個參數,這個參數是 coroutine 的主函數。 create 函數僅僅建立一個新的 coroutine 而後返回它的控制器 (一個類型爲 thread 的對象); 它並不會啓動 coroutine 的運行。rest
當你第一次調用 coroutine.resume 時, 所需傳入的第一個參數就是 coroutine.create 的返回值。 這時,coroutine 從主函數的第一行開始運行。 接下來傳入 coroutine.resume 的參數將被傳進 coroutine 的主函數。 在 coroutine 開始運行後,它講運行到自身終止或是遇到一個 yields 。code
coroutine 能夠經過兩種方式來終止運行: 一種是正常退出,指它的主函數返回(最後一條指令被運行後,不管有沒有顯式的返回指令); 另外一種是非正常退出,它發生在未保護的錯誤發生的時候。 第一種狀況中, coroutine.resume 返回 true , 接下來會跟着 coroutine 主函數的一系列返回值。 第二種發生錯誤的狀況下, coroutine.resume 返回 false , 緊接着是一條錯誤信息。orm
coroutine 中切換出去,能夠調用 coroutine.yield。 當 coroutine 切出,與之配合的 coroutine.resume 就當即返回, 甚至在 yield 發生在內層的函數調用中也能夠(就是說, 這不限於發生在主函數中,也能夠是主函數直接或間接調用的某個函數裏)。 在 yield 的狀況下,coroutine.resume 也是返回 true, 緊跟着那些被傳入 coroutine.yield 的參數。 等到下次你在繼續一樣的 coroutine ,將從調用 yield 的斷點處運行下去。 斷點處 yield 的返回值將是 coroutine.resume 傳入的參數。
相似 coroutine.create , coroutine.wrap 這個函數也將建立一個 coroutine , 可是它並不返回 coroutine 自己,而是返回一個函數取而代之。一旦你調用這個返回函數,就會切入 coroutine 運行。 全部傳入這個函數的參數等同於傳入 coroutine.resume 的參數。 coroutine.wrap 會返回全部應該由除第一個(錯誤代碼的那個布爾量) 以外的由 coroutine.resume 返回的值。 和 coroutine.resume 不一樣, coroutine.wrap 不捕獲任何錯誤; 全部的錯誤都應該由調用者本身傳遞。
Lua語言實現的協程是一種非對稱式(asymmetric)協程,或稱半對稱式(semi-symmetric)協程,又或乾脆就叫半協程(semi-coroutine)。
這種協程機制之因此被稱爲非對稱的,是由於它提供了兩種傳遞程序控制權的操做:
一個非對稱協程能夠看作是從屬於它的調用者的,兩者的關係很是相似於例程(routine)與其調用者之間的關係。既然有非對稱式協程,固然也就有對稱式(symmetric)協程了,它的特色是隻有一種傳遞程序控制權的操做,即將控制權直接傳遞給指定的協程。曾經有這麼一種說法,對稱式和非對稱式協程機制的能力並不等價,但事實上很容易根據前者來實現後者。
coroutine.create (f)
Creates a new coroutine, with body f. f must be a Lua function. Returns this new coroutine, an object with type "thread".
coroutine.resume (co [, val1, ···])
Starts or continues the execution of coroutine co. The first time you resume a coroutine, it starts running its body. The values val1, ··· are passed as the arguments to the body function. If the coroutine has yielded, resume restarts it; the values val1, ··· are passed as the results from the yield.
If the coroutine runs without any errors, resume returns true plus any values passed to yield (if the coroutine yields) or any values returned by the body function (if the coroutine terminates). If there is any error, resume returns false plus the error message.
coroutine.running ()
Returns the running coroutine, or nil when called by the main thread.
coroutine.status (co)
Returns the status of coroutine co, as a string: "running", if the coroutine is running (that is, it called status); "suspended", if the coroutine is suspended in a call to yield, or if it has not started running yet; "normal" if the coroutine is active but not running (that is, it has resumed another coroutine); and "dead" if the coroutine has finished its body function, or if it has stopped with an error.
coroutine.yield (···)
Suspends the execution of the calling coroutine. The coroutine cannot be running a C function, a metamethod, or an iterator. Any arguments to yield are passed as extra results to resume.
coroutine.wrap (f)
Creates a new coroutine, with body f. f must be a Lua function. Returns a function that resumes the coroutine each time it is called. Any arguments passed to the function behave as the extra arguments to resume. Returns the same values returned by resume, except the first boolean. In case of error, propagates the error.