Lua C - Is there a way to keep a value in C, but still have it not garbage collected?

I am creating a thread in lua c, but i want to keep it just in C, without making it a variable in the environment/etc. But when i throw away the thread value that gets pushed by lua_newthread, it gets garbace collected shortly after, and so becomes useless. In this thread i want to run arbitrary lua code passed by the user, allowing them to use the exclusive functions/variables that are isolated from other threads.

Is there a way to mark a thread/userdata/table value as non-garbage-collectable?


  • That is possible. You can add a call to gc_mark with your TValue's gcobj as an argument into gc_markroots.