Lua 的 Coroutine(协程)模块

基本概念

Lua 的 Coroutine(协程)是一种轻量级的线程,它允许程序在单个线程中实现多个执行流的协作式调度。与操作系统线程不同,协程是完全由用户控制的,在用户态进行切换,不需要内核参与。

核心功能

创建协程

lua 复制代码
co = coroutine.create(function()
    print("协程开始执行")
end)

启动/恢复协程

lua 复制代码
coroutine.resume(co)  -- 输出:"协程开始执行"

挂起协程

lua 复制代码
co = coroutine.create(function()
    print("第一步")
    coroutine.yield()
    print("第二步")
end)

coroutine.resume(co)  -- 输出:"第一步"
coroutine.resume(co)  -- 输出:"第二步"

状态管理

协程有以下几种状态:

  • suspended(挂起):刚创建或调用 coroutine.yield 后的状态
  • running(运行):正在执行的状态
  • dead(结束):函数执行完毕的状态

可以通过 coroutine.status(co) 查询协程状态。

数据交换

协程支持在 coroutine.resumecoroutine.yield 之间传递数据:

lua 复制代码
co = coroutine.create(function(x)
    print("收到:"..x)
    local y = coroutine.yield("返回1")
    print("收到:"..y)
    return "返回2"
end)

print(coroutine.resume(co, "输入1"))  -- 输出:"收到:输入1" 和 "true 返回1"
print(coroutine.resume(co, "输入2"))  -- 输出:"收到:输入2" 和 "true 返回2"

应用场景

  1. 迭代器实现:可以用协程实现复杂的迭代逻辑
  2. 状态机:将状态转换逻辑封装在协程中
  3. 协作式多任务:在单线程中模拟多任务处理
  4. 游戏开发:处理角色AI、动画序列等
  5. 网络编程:实现非阻塞IO的协程调度

示例:生产者-消费者模式

lua 复制代码
function producer()
    return coroutine.create(function()
        while true do
            local x = io.read()
            coroutine.yield(x)
        end
    end)
end

function consumer(prod)
    while true do
        local status, value = coroutine.resume(prod)
        if not status then break end
        print("消费:"..value)
    end
end

consumer(producer())

注意事项

  1. 协程不是抢占式的,需要显式调用 coroutine.yield 让出执行权
  2. 协程的栈空间有限,深度递归可能导致栈溢出
  3. 协程间的数据共享需要注意同步问题
  4. 错误处理需要通过 coroutine.resume 的返回值判断

扩展阅读

Lua 5.3+ 版本对协程做了优化,性能更好。在 LuaJIT 中协程的执行效率更高,适合高性能场景。

相关推荐
s1hiyu5 分钟前
C++动态链接库开发
开发语言·c++·算法
(❁´◡`❁)Jimmy(❁´◡`❁)6 分钟前
CF2188 C. Restricted Sorting
c语言·开发语言·算法
星火开发设计11 分钟前
C++ 预处理指令:#include、#define 与条件编译
java·开发语言·c++·学习·算法·知识
许泽宇的技术分享13 分钟前
第 1 章:认识 Claude Code
开发语言·人工智能·python
AIFQuant27 分钟前
如何利用免费股票 API 构建量化交易策略:实战分享
开发语言·python·websocket·金融·restful
Hx_Ma1628 分钟前
SpringMVC返回值
java·开发语言·servlet
BugShare30 分钟前
Obsidian 使用指南:从零开始搭建你的个人知识库
笔记·obsidian
独自破碎E34 分钟前
【滑动窗口+字符计数数组】LCR_014_字符串的排列
android·java·开发语言
2601_9494800640 分钟前
【无标题】
开发语言·前端·javascript
Jack_David44 分钟前
Java如何生成Jwt之使用Hutool实现Jwt
java·开发语言·jwt