加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

lua – 放弃协同程序

发布时间:2020-12-15 00:25:28 所属栏目:大数据 来源:网络整理
导读:在Lua 5.1中,从不让一个协程正常结束有多糟糕?换句话说,如果一个协程产生但是我从未恢复它,它是否会在程序完成之前留下很多状态? cor=coroutine.wrap(somefunc)while true do done=cor() if done then -- coroutine exited with "return true" break else
在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页)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读