Will yielding coroutines get garbage collected if they arent connected to any variables anymore?

i have a script that utilizes recursive functions to calculate what im trying to achieve but i need the ability to kill those functions without returning thousands of times within each function-layer i run.

i run 16 of those functions in their own coroutines all within a table so i can tell when they’re all finished calculating what im trying to calculate (yes conditional recursion is the best method available) so would they get garbage collected if i yielded them all and removed them from the table? trying to kill a function that can call itself up to (14^2)-20 times via returns seems like an absolute pain there has to be a better way

to make it easier to understand this is the sorta stuff im talking about

function a(starting variables)
    wait()
    --some calculations that requires recursion
    --some conditions
    a(variables i pass)
end

coroutines = {}
for i = 1,16 do
     local Co = coroutine.create(a(variables))
     coroutine.resume(Co)
     table.insert(coroutines,Co)
end
repeat
    for i,v in pairs(coroutines)do
        if coroutine.status(v) == "dead" then
            table.remove(coroutines,i)
        end
    end
    wait()
until #coroutines == 0

this is the thing i have roughly i need to know how i can kill these coroutines and make sure they’re gone from the memory when an event fires without having to return thousands of times within each recursive function

I don’t think so, you’d just be getting rid of the coroutine object reference. From my knowledge, I don’t believe there’s any way to actually kill a thread, although that’d be a really cool feature.

so ig my best option is simply using error() via an if statement on a part of the function that runs frequently so the entire function stack would die killing the coroutine hopefully :thinking:

1 Like