lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I think i found the result:
 
    from manual:"Creates a new thread, pushes it on the stack, and returns a pointer to a lua_State that represents this new thread."
    if the thread object is in stack,it means it is my duty to keep the stack variable be referanced by somwhere in VM ,like store the stack element into some global scope variable(as ketmar dark says,registry) to keep the thread alive.
   
for ketmar dark:
    "add second call to gc and enjoy the bugs. %-)"
    i have add a sequnce of lua_gc ,and it is still ready for use.

                      thanks for help,i think i am clear now.

From: lvyou@msn.com
To: lua@bazar2.conectiva.com.br
Subject: when a lua thread created from c can be release?
Date: Mon, 13 Aug 2007 14:36:06 +0800

hello everyone:

     When i use lua_newthread to create a lua thread,i will got a lua_State pointer.
     from manual, it is said:"There is no explicit function to close or to destroy a thread. Threads are subject to garbage collection, like any Lua object".
     Problem is:I kept a lua_State pointer (for the newly created thread) outside the VM,and I have no way to tell VM when i don't need it.if the thread been sweeped as garbage,does the pointer i kept still valid? in other word,both my code and VM doesn't know when this thread will become really useless.
     I just want to know how does the lua VM deal with this problem.
 

here is my test code just for reference:
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);
    lua_State* T = lua_newthread(L);
    lua_gc(L,LUA_GCCOLLEC T,0);
    luaL_dofile(T,"test.lua");
after invoke lua_gc, this new thread pointer T still valid, and test.lua executed successfully.
 
                                      thanks


中国十大热门旅游景点 了解更多信息!