[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re[2]: GC and threads
- From: Dmitriy Iassenev <iassenev@...>
- Date: Wed, 28 Apr 2004 17:58:00 +0300
Hello RLake,
Wednesday, April 28, 2004, 5:05:20 PM, you wrote:
So, is it safe to do the following:
// creating thread
m_thread = lua_newthread(L);
m_thread_reference = luaL_ref(L,-1);
lua_pop (L,1);
// killing finished or yielded thread
lua_pushlightuserdata (L,m_thread);
luaL_unref (L,-1,m_thread_reference);
lua_pop (L,1);
Roop> If there is no reference to the thread in Lua, the
Roop> thread may be garbage
Roop> collected at any moment (except when it is running).
Roop> So you must keep a
Roop> reference to it.
Roop> You should probably use luaL_ref and keep the reference
Roop> indexes in your
Roop> C++ objects.
Roop> When you call lua_newthread(L), the new thread object
Roop> will be placed on L's
Roop> stack. If you don't pop it from that stack, then the
Roop> thread is not garbage
Roop> collectable, assuming that L is not garbage collectable
Roop> (i.e., L is the
Roop> main thread). Of course, it may not be a good idea
Roop> to just let things
Roop> accumulate on the main stack.
Roop> A thread is garbage collectable precisely when no
Roop> references to it remain
Roop> (in Lua), regardless of its "state". (Threads
Roop> in Lua are not really threads
Roop> the way you might be thinking of them; they don't
Roop> really "block". The best
Roop> way to think of them, imho, is as separate execution
Roop> stacks.)
Roop> From the stack trace, it looks to me like the thread
Roop> is running and is trying
Roop> to put something in a table, rather than crashing
Roop> on the invocation of pcall.
Roop> It is hard to know what the problem is; it may or
Roop> may not be related to
Roop> garbage collection.
--
Best regards,
Dmitriy mailto:iassenev@gsc-game.kiev.ua