关于lua的协程的问题:为什么主线程不';当另一个线程屈服时,是否继续运行?
当另一个线程屈服时,为什么主线程不继续运行 你看,终端上没有“你好世界”关于lua的协程的问题:为什么主线程不';当另一个线程屈服时,是否继续运行?,lua,Lua,当另一个线程屈服时,为什么主线程不继续运行 你看,终端上没有“你好世界” function foo () print("foo" ) count = 0; while( 0<1 ) do count = count + 1; print("count=", count); if count>5 then break end end
function foo ()
print("foo" )
count = 0;
while( 0<1 )
do
count = count + 1;
print("count=", count);
if count>5 then
break
end
end
return coroutine.yield()
end
co = coroutine.create(function ()
foo();
print("hello world") --why doesn't "hello world" output to the terminal?
print(type(co))
return b, "end"
end)
coroutine.resume(co)
函数foo()
打印(“foo”)
计数=0;
而(05),
打破
结束
结束
return-coroutine.yield()
结束
co=coroutine.create(函数()
foo();
打印(“hello world”)-“hello world”为什么不输出到终端?
印刷品(打字(co))
返回b,“结束”
(完)
合作程序。简历(co)
增加:
这个代码片段(添加一行代码)似乎很有效,但我不完全理解为什么
function foo ()
print("foo" )
count = 0;
while( 0<1 )
do
count = count + 1;
print("count=", count);
if count>5 then
break
end
end
return coroutine.yield()
end
co = coroutine.create(function ()
foo();
print("hello world")
print(type(co))
return b, "end"
end)
coroutine.resume(co)
coroutine.resume(co) --add this line
函数foo()
打印(“foo”)
计数=0;
而(05),
打破
结束
结束
return-coroutine.yield()
结束
co=coroutine.create(函数()
foo();
打印(“你好世界”)
印刷品(打字(co))
返回b,“结束”
(完)
合作程序。简历(co)
恢复(co)--添加此行
控制台中没有“hello world”
,因为协同程序在打印之前生成
co = coroutine.create(function ()
foo(); -- <-- coroutine.yield() inside!
print("hello world")
print(type(co))
return b, "end"
end)
co=coroutine.create(函数()
foo();--还要记住,lua协程不是真正的线程