Refuse coroutines

How bad is it in Lua 5.1 to never let a coroutine end properly? In other words, if coroutine gives, but I never resume it, will it leave a lot of state lying until the program ends?

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 

Depending on some_condition in the pseudo-code above, the coroutine can never be resumed and, therefore, can never “finish”.

Can I do this with dozens of coroutines without worrying? Is it possible to leave coroutines in this state? It is expensive?

+7
coroutine lua
source share
1 answer

The garbage collector can easily determine that the coroutine is unavailable and collects it. I don’t know if any of the documents state that this will happen, but I tried the “empirical method”:

 while true do
   local cor = coroutine.wrap (function () coroutine.yield (false) end)
   cor ()
 end

Memory usage has not increased over time.

Edit: Google says:

There is no explicit operation to remove Lua coroutine; like any other value in Lua, coroutines are discarded by garbage collection. (Page 4 in PDF)

+17
source share

All Articles