一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Lua - Lua之協同程序coroutine代碼實例

Lua之協同程序coroutine代碼實例

2020-04-24 11:19腳本之家 Lua

這篇文章主要介紹了Lua之協同程序coroutine代碼實例,本文給出的代碼示例較為復雜,需要對Lua協同程序有一定的了解方能看懂,需要的朋友可以參考下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
do
    --create coroutine table
    --coroutine state: suspended, running, dead, normal
    --when create the coroutine, the status is suspended, After calling it, the status is dead
    --get the coroutine status by the way coroutine.status
    local coA = 0;
    local coB = 0;
 
    function createCoroutineA()
 
        coA = coroutine.create(
                                        function()
                                            --for i = 0, 10 do
                                                print("coA: ", 0);
                                                print("coB status: ", coroutine.status(coB)); --normal status
                                                print("coA status: ", coroutine.status(coA));
                                                print("coA coroutine next status");
                                                --coroutine.yield();--the current coroutine is suspended
                                            --end
                                        end
                                    );
        print("From coA to resume coB");
    end
 
 
    function createCoroutineB()
 
        coB = coroutine.create(
                                        function()
                                            --for i = 0, 10 do
                                                print("coB: ", 0);
                                                print("coA status: ", coroutine.status(coA));
                                                coroutine.resume(coA); --when resume coA, the coB will suspended, calling coB ,the coA status is
                                                --suspended and dead, this time will continue to execute the next code
                                                print("coB status: ", coroutine.status(coB));
                                                print("coB coroutine next status");
                                                --coroutine.yield();
                                            --end
                                        end
                                    );
        print("From coB to resume coA");
    end
 
    --display the coA and coB status
    createCoroutineA();
    print(coroutine.status(coA));
 
    createCoroutineB();
    print(coroutine.status(coB));
 
    coroutine.resume(coB);
    print(coroutine.resume(coB)); --if the coroutine is dead ,the resume will resume false, and can't resume the dead coroutine
    --print("coA status: ", coroutine.status(coA));
    --print("coB status: ", coroutine.status(coB));
end

注:
resume得到返回值,
如果有對應的yield在wait resume,那么yield的參數作為resum的返回值,第一個返回值表示coroutine沒有錯誤,后面的返回值個數及其值視yeild參數而定。
如果沒有yield在wait,那么返回值是對應函數的返回值,:true,* * *

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
do
    --create coroutine table
    --coroutine state: suspended, running, dead, normal
    --when create the coroutine, the status is suspended, After calling it, the status is dead
    --get the coroutine status by the way coroutine.status
    local coA = 0;
    local coB = 0;
 
    function createCoroutineA()
 
        coA = coroutine.create(
                                        function(paramA, paramB)
                                            --for i = 0, 10 do
                                                print("coA: ", 0);
                                                coroutine.yield(paramA, paramB);--the current coroutine is suspended
                                            --end
                                            return 100, 200;
                                        end
                                    );
        print("From coA to resume coB");
    end
 
 
    function createCoroutineB()
 
        coB = coroutine.create(
                                        function()
                                            --for i = 0, 10 do
                                                print("coB: ", 0);
                                                print("coA status: ", coroutine.status(coA));
                                                coroutine.resume(coA); --when resume coA, the coB will suspended, calling coB ,the coA status is
                                                --suspended and dead, this time will continue to execute the next code
                                                print("coB status: ", coroutine.status(coB));
                                                print("coB coroutine next status");
                                                --coroutine.yield();
                                            --end
                                        end
                                    );
        print("From coB to resume coA");
    end
    createCoroutineA();
    --if not yield is waiting ,the return values that the main function return as the results of the resume
    --or the return as the yield params
    print( coroutine.resume(coA, 10, 20));--OutPut:true, 10, 20
 
 
 
end

 

延伸 · 閱讀

精彩推薦
  • LuaLua教程(二):基礎知識、類型與值介紹

    Lua教程(二):基礎知識、類型與值介紹

    這篇文章主要介紹了Lua教程(二):基礎知識、類型與值介紹,本文講解了Hello World程序、代碼規范、全局變量、類型與值等內容,需要的朋友可以參考下 ...

    腳本之家5922020-04-28
  • LuaLua和C語言的交互詳解

    Lua和C語言的交互詳解

    這篇文章主要介紹了Lua和C語言的交互詳解,Lua和C語言通過棧完成交互,本文結合代碼實例詳細講解了交互的方法,需要的朋友可以參考下 ...

    果凍想3702020-04-14
  • LuaLua中的元方法__newindex詳解

    Lua中的元方法__newindex詳解

    這篇文章主要介紹了Lua中的元方法__newindex詳解,本文講解了查詢與更新、監控賦值、通過table給另一個table賦值等內容,需要的朋友可以參考下 ...

    笨木頭8872020-04-09
  • LuaLua中table庫函數方法介紹

    Lua中table庫函數方法介紹

    這篇文章主要介紹了Lua中table庫函數方法介紹,本文講解了concat、insert、maxn、remove、sort、foreachi等方法,需要的朋友可以參考下 ...

    腳本之家2502020-04-17
  • LuaLua中計算、執行字符串中Lua代碼的方法

    Lua中計算、執行字符串中Lua代碼的方法

    這篇文章主要介紹了Lua中計算、執行字符串中Lua代碼的方法,類似JavaScript中eval函數的功能,在Lua中也可以實現,需要的朋友可以參考下 ...

    腳本之家6322020-04-30
  • LuaLua實現__add方法重載示例

    Lua實現__add方法重載示例

    這篇文章主要介紹了Lua實現__add方法重載示例,本文直接給出實現代碼,需要的朋友可以參考下 ...

    腳本之家7452020-04-24
  • Lua深入探究Lua中的解析表達式

    深入探究Lua中的解析表達式

    這篇文章主要介紹了深入探究Lua中的解析表達式,對于其語法部分的說明和示例都超詳細,極力推薦此文!需要的朋友可以參考下 ...

    腳本之家3542020-05-05
  • LuaLua簡介、編譯安裝教程及變量等語法介紹

    Lua簡介、編譯安裝教程及變量等語法介紹

    這篇文章主要介紹了Lua簡介、編譯安裝教程及變量等語法介紹,本文同時講解了lua注釋語法、Lua命令行方式等內容,需要的朋友可以參考下 ...

    junjie3632020-04-14
主站蜘蛛池模板: 精品国产免费第一区二区三区日韩 | 微拍秒拍99福利精品小视频 | 2023毛片| 极品在线 | 精品国产一区二区三区在线观看 | 欧美日韩亚毛片免费观看 | 国产成人精品免费视频大全五级 | 毛片99| 欧美日韩国产一区二区三区不卡 | 九九九久久久 | 色中文网| 四虎免费影院4hu永久免费 | 国产综合成色在线视频 | 亚洲日本中文字幕天天更新 | 久久青草费线频观看国产 | 日韩欧美色 | 国产成人精品免费视频软件 | 女人肮脏的交易中文字幕未删减版 | 日本人泡妞18xxⅹ | 精品九九视频 | 99热久久国产精品这里 | 午夜免费无码福利视频麻豆 | 草嫩社区 | 精品国产品国语在线不卡丶 | 成人永久免费视频 | 男女激情网 | 国产成人精品高清免费 | 高人先生免费观看全集 | 美人老师沦为 | 日日碰日日操 | 亚洲嫩模吧粉嫩粉嫩冒白浆 | 欧美一级在线播放 | 欧美高清在线不卡免费观看 | 欧美在线看片a免费观看 | 日本中年japanesebear | 日韩大片免费看 | 亚洲成人综合在线 | 国内精品91东航翘臀女神在线 | 国产在线激情视频 | 91精品国产亚洲爽啪在线影院 | 精品国产区一区二区三区在线观看 |