I have a module that allows Lua to create some objects. Each of those objects has its own OS thread doing some work. Even if the Lua state that created them discards the reference to them, I want those threads to run to completion. That's simple enough; just make the Lua objects only pointers to them, and don't delete them when the reference is collected.
The problem is, they do need to be deleted eventually, and that might be long after the Lua state that created them has discarded and collected all references to them. I don't think it'd be terribly easy for them to clean themselves up, either - the thread that created them should be the one to delete them (or else the design becomes a lot more complicated).
What seems like the best option is to have the parent Lua state periodically call a "cleanup" function, which will go through all objects which are ready to be disposed (thread has finished and Lua has collected the references) and delete them.
From there, it seems like a no-brainer: Lua already periodically calls a function to clean up its own garbage. Is there some nice way to get a callback for each garbage collection cycle, so I can clean up these objects too?