Lua是否应该放弃使用协程?
- 内容介绍
- 文章标签
- 相关推荐
本文共计397个文字,预计阅读时间需要2分钟。
在Lua 5.1中,如果让一个协程正常结束,是否会有很多余的操作?
假设一个协程生成后,是我从未恢复它,那么它是否会在程序完成前留下很多状态?
luacor=coroutine.wrap(somefunc)while true do done=cor() if done then endend
在Lua 5.1中,从不让一个协程正常结束有多糟糕?换句话说,如果一个协程产生但是我从未恢复它,它是否会在程序完成之前留下很多状态?cor=coroutine.wrap(somefunc) while true do done=cor() if done then -- coroutine exited with "return true" break else -- coroutine yielded with "coroutine.yield(false)" if some_condition then break end end end function somefunc() -- do something coroutine.yield(false) -- do some more return true end
根据上面伪代码中的some_condition,协程可能永远不会被恢复,因此可能永远不会正确地“结束”.
我可以这样做几十个协同程序而不必担心吗?将协同程序置于此状态是否安全?这个很贵吗?
垃圾收集器可以很容易地确定协程无法访问并收集它.我不知道是否有任何文件表明会发生这种情况,但我尝试了“经验方法”:while true do local cor = coroutine.wrap(function() coroutine.yield(false) end) cor() end
内存使用率并未随着时间的推移而增
编辑:谷歌说:
There is no explicit operation for deleting a Lua coroutine; like any other value in Lua, coroutines are discarded by garbage collection.(PDF中的第4页)
本文共计397个文字,预计阅读时间需要2分钟。
在Lua 5.1中,如果让一个协程正常结束,是否会有很多余的操作?
假设一个协程生成后,是我从未恢复它,那么它是否会在程序完成前留下很多状态?
luacor=coroutine.wrap(somefunc)while true do done=cor() if done then endend
在Lua 5.1中,从不让一个协程正常结束有多糟糕?换句话说,如果一个协程产生但是我从未恢复它,它是否会在程序完成之前留下很多状态?cor=coroutine.wrap(somefunc) while true do done=cor() if done then -- coroutine exited with "return true" break else -- coroutine yielded with "coroutine.yield(false)" if some_condition then break end end end function somefunc() -- do something coroutine.yield(false) -- do some more return true end
根据上面伪代码中的some_condition,协程可能永远不会被恢复,因此可能永远不会正确地“结束”.
我可以这样做几十个协同程序而不必担心吗?将协同程序置于此状态是否安全?这个很贵吗?
垃圾收集器可以很容易地确定协程无法访问并收集它.我不知道是否有任何文件表明会发生这种情况,但我尝试了“经验方法”:while true do local cor = coroutine.wrap(function() coroutine.yield(false) end) cor() end
内存使用率并未随着时间的推移而增
编辑:谷歌说:
There is no explicit operation for deleting a Lua coroutine; like any other value in Lua, coroutines are discarded by garbage collection.(PDF中的第4页)

