lua协程实例

一、lua协程的和c++中协程的区分

      1. lua 程序设计中的关于lua多线程以及协程的概述

上述说的意思我认为就是  lua的协程类似于但是不等同于真正意义的多线程(同时执行一些操作);我做过测试,同时创建1000个协程并执行(代码执行的速度可以忽略不计的),所有的创建成功的时间不是在同一毫秒内,所以,协程之间按照顺序并且不会相互影响的。

2. 协程几个主要函数

   

协程的函数都是封装在 lua api中 coroutine 表中(下面是api,加翻译,我做了一些修改,感觉这个东西已经讲解的比较清楚,不用我在讲解了。)
coroutine 函数

协程的函数都是封装在 lua api中 coroutine 表中
coroutine 函数
--[Comment]
--The operations related to coroutines comprise a sub-library of the basic library and come inside the table coroutine.
--与协程相关的操作包括基本库的子库,并且位于表协程内部。
coroutine = {};

--[Comment]
--Creates a new coroutine, with body f. f must be a Lua function. Returns this new coroutine, an object with type "thread".
--创建一个新的协程,其主体为f。 f必须是Lua函数。返回此新协程,即类型为“线程”的对象
function coroutine.create (f) end

--[Comment]
--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.
--
--开始或继续执行协程co。首次恢复协程时,它会开始运行它的函数体。值val1,...作为参数传递给body函数。如果协程已暂停,则resume重启它;值val1,···作为yield的返回值被传递。
--
--如果协程运行时没有任何错误,resume将返回true以及传递给yield的任何值(如果协程暂停的话),或者函数体返回的任何值(如果协程终止)。如果有任何错误,resume将返回false和错误消息。
function coroutine.resume (co , val1, ...) end

--[Comment]
--Returns the running coroutine, or nil when called by the main thread.
--返回正在运行的协程,如果由主线程调用则返回nil。
function coroutine.running () end

--[Comment]
--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.
--
如果协程正在运行,则以字符串形式返回协程co的状态:“ running”(即,它称为status); “已暂停”,如果协程在调用yield时被暂停,或者尚未开始运行;如果协程处于活动状态但未运行(即已恢复另一个协程),则为“normal”;如果协程已经执行完其函数体代码,或者由于错误而停止,则为“dead”。
function coroutine.status (co) end

--[Comment]
--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.

--创建一个新的协程,其主体为f。 f必须是Lua函数。返回一个函数,该函数在每次调用协程时都将其恢复。传递给函数的所有参数都将作为要恢复的额外参数。返回由简历返回的相同值,除了第一个布尔值。发生错误时,传播错误。
function coroutine.wrap (f) end

--[Comment]
--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.
--
--暂停调用协程的执行。协程不能运行C函数,元方法或迭代器。传递yield的任何参数作为要恢复的额外结果。
function coroutine.yield (...) end

3. 附带两个实例

    实例一(这个是从某个博客上找到的,具体那个给忘记了,作者或者哪个人看到了,请私信我,我附带上)

    

function status()
    print("co1's status :"..coroutine.status(co1).." ,co2's status: "..coroutine.status(co2))
end

co1 = coroutine.create(function ( a )
    print(" co1 arg is :"..a)
    status()
    local stat,rere = coroutine.resume(co2,"2")
    print("****************************************");
    print("co1 resume's return is "..rere)
    status()
    local stat2,rere2 = coroutine.resume(co2,"4")
    print("co1 resume's return is "..rere2)
    local arg = coroutine.yield("6")
end)
co2 = coroutine.create(function ( a )
    print(" co2 arg is :"..a)
    status()
    local rey = coroutine.yield("3")
    print("co2 yeild's return is " .. rey)
    status()
    coroutine.yield("5")
end)
--主线程执行co1,传入字符串“main thread arg”
stat,mainre = coroutine.resume(co1,"1")
status()
print("last return is "..mainre)

实例二(云风 大神写的例子 )

--云风
print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 云风");
function foo(a)
    print("%%%%%%%%");
    print("********" .. a);
    print("foo", a)
    return coroutine.yield(2 * a)
end

co = coroutine.create(function ( a, b )
    --main1
    print("co-body", a, b)
    local r = foo(a + 1)

    --mian2
    print("co-body", r)
    local r, s = coroutine.yield(a + b, a - b)
    --main3 
    print("co-body", r, s)
    return b, "end"
end)

print("main1", coroutine.resume(co, 1, 10))
print("main2", coroutine.resume(co, "r"))
print("main3", coroutine.resume(co, "x", "y"))
print("main4", coroutine.resume(co, "x", "y"))

实例三(lua程序设计的实例,我自己也是按照这个例子编码了一个 压力测试工具)

            具体的实例是参照 lua程序设计中编码(具体的实例参照该书第九章的 下载文件实例,本人完全参考这个实例编码了一个socket多线程测试脚本,附带这本书的 百度云链接:https://pan.baidu.com/s/1JADICzJX4X7c42IWvya9iQ,提取码:9pb6),有兴趣的可以参考书上例子进行编码。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值