# 【深入Lua】理解Lua中最强大的特性-coroutine（协程）

2013/12/21 00:40

###coroutine基础

Lua将它的协程函数都放进了coroutine这个表里，其中主要的函数如下

``````<!-- lang: lua -->
function foo(a)
print("foo", a)
return coroutine.yield(2 * a)
end

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

print("main", coroutine.resume(co, 1, 10))
print("main", coroutine.resume(co, "r"))
print("main", coroutine.resume(co, "x", "y"))
print("main", coroutine.resume(co, "x", "y"))
``````

``````co-body 1 10
foo 2
main true 4
co-body r
main true 11, -9
co-body x y
main false 10 end
main false cannot resume dead coroutine
``````

###coroutine和subroutine(子例程)的比较

``````var q = queue()
``````

``````loop
while q is not full
create product
resume to consumer
``````

``````loop
while q is not empty
consume product
remove the items from q
yield
``````

###coroutine的和callback的比较

coroutine经常被用来和callback进行比较，因为通常来说，coroutine和callback可以实现相同的功能，即异步通信，比如说下面的这个例子：

``````<!-- lang: lua -->
bob.walkto(jane)
bob.say("hello")
jane.say("hello")
``````

``````<!-- lang: lua -->
bob.walto(function (  )
bob.say(function (  )
jane.say("hello")
end,"hello")
end, jane)
``````

``````<!-- lang: lua -->
co = coroutine.create(function (  )
local current = coroutine.running
bob.walto(function (  )
coroutine.resume(current)
end, jane)
coroutine.yield()
bob.say(function (  )
coroutine.resume(current)
end, "hello")
coroutine.yield()
jane.say("hello"）
end)

coroutine.resume(co)
``````

``````<!-- lang: lua -->
function runAsyncFunc( func, ... )
local current = coroutine.running
func(function (  )
coroutine.resume(current)
end, ...)
coroutine.yield()
end

coroutine.create(function (  )
runAsyncFunc(bob.walkto, jane)
runAsyncFunc(bob.say, "hello")
jane.say("hello")
end)

coroutine.resume(co)
``````

8
28 收藏

### 作者的其它热门文章

#### 引用来自“王选易”的评论

coroutine.resume写错了，是后一个yield

2016/12/01 17:04

2016/02/19 18:28

coroutine.resume写错了，是后一个yield
2014/04/16 08:16

2013/12/21 15:44

2013/12/21 15:34

#### 引用来自“ChildhoodX”的评论

local co = coroutine.create(
function()
print("hi")
end
)

print(type(co))
print(co)
print(coroutine.running())

coroutine.resume(co)
print(coroutine.running())

print(coroutine.status(co))

------------------------------输出：
hi

local co = coroutine.create(
function()
print("hi")
print(coroutine.running())
end
)

2013/12/21 15:23

2013/12/21 15:02

#### 引用来自“ChildhoodX”的评论

local co = coroutine.create(
function()
print("hi")
end
)

print(type(co))
print(co)
print(coroutine.running())

coroutine.resume(co)
print(coroutine.running())

print(coroutine.status(co))

------------------------------输出：
hi

local co = coroutine.create(
function()
print("hi")
print(coroutine.running())
end
)

2013/12/21 14:49

#### 引用来自“ChildhoodX”的评论

local co = coroutine.create(
function()
print("hi")
end
)

print(type(co))
print(co)
print(coroutine.running())

coroutine.resume(co)
print(coroutine.running())

print(coroutine.status(co))

------------------------------输出：
hi

local co = coroutine.create(
function()
print("hi")
print(coroutine.running())
end
)
2013/12/21 14:20

nil
hi
nil