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
|
co=coroutine.create(function() print( "hi" ) end) print(coroutine.status(co)) coroutine.resume(co) print(coroutine.status(co)) print() co=coroutine.create(function() for i=1,2 do print( "co" ,i) coroutine.yield() end end) coroutine.resume(co) print(coroutine.status(co)) coroutine.resume(co) print(coroutine.status(co)) coroutine.resume(co) --没有输出 print(coroutine.status(co)) print() co=coroutine.create(function(a,b,c) print( "co" ,a,b,c) end) coroutine.resume(co,1,2,3) co=coroutine.create(function(a,b) print( "I'm before yield" ) --第一次运行执行 coroutine.yield(a+b,a-b, "needless args" ) --在这停住,返回yield的参数 print( "Mgs" ) end) print(coroutine.resume(co,20,10)) --参数传给yield,处理后再返回 print( "I print first" ) coroutine.resume(co) co=coroutine.create(function() return "I'll return" end) print(coroutine.resume(co)) --主函数的返回值回传给resume print() |
输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
suspended hi dead co 1 suspended co 2 suspended dead co 1 2 3 I'm before yield true 30 10 needless args I print first Mgs true I'll return |