当前位置:网站首页>Lua 快速入门(五)——协程(thread)
Lua 快速入门(五)——协程(thread)
2022-08-11 05:31:00 【canon_卡农】
前言
Lua是由标准C实现的解释型语言,它的执行效率比较高,通常作为脚本嵌入到应用程序中。
由于支持热更新,Lua在游戏开发领域应用普遍。作为从业人员,不得不来填这个大坑。
本文是Lua入门学习笔记,来源是泰课的一个Lua教程。这里主要记录Lua的语法,以及实现
简单的OOP。
Lua 快速入门(一)——基础语法
Lua 快速入门(二)——函数(Function)
Lua 快速入门(三)——表(Table)
Lua 快速入门(四)——多脚本执行
Lua 快速入门(五)——协程(thread)
Lua 快速入门(六)——基础OOP
协程(Thread)
Lua将所有关于协同程序的函数放置在一个名为“coroutine”的table中。
1、coroutine.create创建一个thread类型的值表示新的协同程序,返回一个协同程序。
2、coroutine.status检查协同程序的状态(挂起suspended、运行running、死亡dead、正常normal)。
3、coroutine.resume启动或再次启动一个协同程序,并将其状态由挂起改为运行。
4、coroutine.yield让一个协同程序挂起。
5、coroutine.wrap同样创建一个新的协同程序,返回一个函数。
print("**********协程的创建************")
--常用方式
--coroutine.create()
fun = function()
print(123)
end
co = coroutine.create(fun)
--协程的本质是一个线程对象
print(co)
print(type(co)) --thread
--coroutine.wrap()
co2 = coroutine.wrap(fun)
print(co2)
print(type(co2)) --function
print("**********协程的运行************")
--第一种方式 对应的 是通过 create创建的协程
coroutine.resume(co)
--第二种方式
co2()
print("**********协程的挂起************")
fun2 = function( )
local i = 1
while true do
print(i)
i = i + 1
print(coroutine.status(co3))
--返回当前正在运行的协程加一个布尔量。 如果当前运行的协程是主线程,返回true。
print(coroutine.running())
coroutine.yield(i)
end
end
co3 = coroutine.create(fun2)
--默认第一个返回值 是 协程是否启动成功
--yield里面的返回值
isOk, tempI = coroutine.resume(co3) -- true 2
print(isOk,tempI)
isOk, tempI = coroutine.resume(co3) -- true 3
print(isOk,tempI)
isOk, tempI = coroutine.resume(co3) -- true 4
print(isOk,tempI)
co4 = coroutine.wrap(fun2)
--这种方式的协程调用 也可以有返回值 只是没有默认第一个返回值了
print(co4()) --2
print(co4()) --3
print(co4()) --4
print("**********协程的状态************")
--coroutine.status(协程对象)
--dead 结束
--suspended 暂停
--running 进行中
print(coroutine.status(co3)) --suspended
print(coroutine.status(co)) --dead
--这个函数可以得到当前正在 运行的协程的线程号
print(coroutine.running())
边栏推荐
- Day 68
- Tinker接入全流程---配置篇
- Use c language to implement tic-tac-toe chess (with source code, you can run it directly)
- Certificate of SearchGuard configuration
- The whole process of Tinker access --- Compilation
- 连接数据库时出现WARN: Establishing SSL connection without server‘s identity verification is not recommended.
- The role of the port
- The whole process of Tinker access --- configuration
- Building a data ecology for feature engineering - Embrace the open source ecology, OpenMLDB fully opens up the MLOps ecological tool chain
- 厂商推送平台-华为接入
猜你喜欢
【LeetCode-56】合并区间
127.0.0.1 connection refused
Node stepping on the pit 80 port is occupied
jdbc接口文档参考,jdbc接口方法逻辑探究
【LeetCod】三数之和-15
OpenMLDB v0.5.0 released | Performance, cost, flexibility reach new heights
Day 76
OpenMLDB Pulsar Connector: Efficiently connect real-time data to feature engineering
微信小程序启动页的实现
Wonderful linkage | OpenMLDB Pulsar Connector principle and practical operation
随机推荐
自己动手写RISC-V的C编译器-02语法描述方法和递归下降解析
Fourth Paradigm OpenMLDB optimization innovation paper was accepted by VLDB, the top international database association
【LeetCode-36】有效的数独
【LeetCode-13】罗马数字转整数
星盟-pwn-fog
C语言预处理
c语言-数据存储部分
Day 85
杀死进程-查看防火墙状态
【LeetCode-73】矩阵置零
Getting Started with JNI
Building a data ecology for feature engineering - Embrace the open source ecology, OpenMLDB fully opens up the MLOps ecological tool chain
Day 71
本地缓存cookie的使用
IndexError: index 9 is out of bounds for axis 0 with size 9;数组下标溢出问题
【LeetCode-147】对链表进行插入排序
无效的修订:3.18.1-g262b901-dirty
OpenMLDB: Consistent production-level feature computing platform online and offline
经纬度求距离
Certificate of SearchGuard configuration