1

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?

Artemking4
  • 31
  • 7

2 Answers2

1

No, you have to reference it somewhere or disable GC. Use lua_ref to reference is in mostly-hidden place: registry. (debug.getregistry())

Spar
  • 1,582
  • 9
  • 16
  • While this is a good answer that would work in many cases - it probably would not for me... I want other threads to have access to the registry too, via debug.getregistry. – Artemking4 Mar 26 '21 at 09:08
  • Do you mean coroutine-like threads or separate lua states? – Spar Mar 26 '21 at 14:40
  • lua_newthread. Not a separate global state but a separate lua state. This is what coroutines use internally, so yes, coroutine-like threads. It would be nearly impossible for me to have different global states if i didnt use lualanes or other stuff. – Artemking4 Mar 26 '21 at 18:57
0

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

Artemking4
  • 31
  • 7