简体   繁体   English

Lua C - 有没有办法在 C 中保留一个值,但仍然没有垃圾收集?

[英]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.我正在 lua c 中创建一个线程,但我想将其保留在 C 中,而不使其成为环境/等中的变量。 But when i throw away the thread value that gets pushed by lua_newthread, it gets garbace collected shortly after, and so becomes useless.但是当我丢弃由 lua_newthread 推送的线程值时,它很快就会被垃圾收集,因此变得无用。 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.在这个线程中,我想运行用户传递的任意 lua 代码,允许他们使用与其他线程隔离的专有函数/变量。

Is there a way to mark a thread/userdata/table value as non-garbage-collectable?有没有办法将线程/用户数据/表值标记为不可回收?

No, you have to reference it somewhere or disable GC.不,您必须在某处引用它或禁用 GC。 Use lua_ref to reference is in mostly-hidden place: registry.使用lua_ref来引用大部分是隐藏的地方:注册表。 ( debug.getregistry() ) ( debug.getregistry() )

That is possible.这是可能的。 You can add a call to gc_mark with your TValue's gcobj as an argument into gc_markroots.您可以使用 TValue 的 gcobj 作为 gc_markroots 的参数添加对 gc_mark 的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM