About lua_newthread

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

About lua_newthread

mos-4

Hi

in lua5.1 manual:

lua_newthread
          lua_State *lua_newthread (lua_State *L);
There is no explicit function to close or to destroy a thread. Threads are
subject to garbage collection, like any Lua object

        I don't understand this , for example like Npc in a game
       
        function AI()
                while(true) do
                        walkto(A)
                        walkto(B)
                        ...
                        walkto(Z)
                end
        end
       
        I bind each npc with a lua_State by lua_newthread

        but the npc will die and i should close the lua_State , but I can
not.
        what can I do ? or some better suggestion ?

Best Regards
mos


Reply | Threaded
Open this post in threaded view
|

Re: About lua_newthread

Raymond Jacobs
When you are done with a thread (when lua_resume returns and it's not a yield), you should remove the thread directly from the stack, this will remove the reference to it and it can then be garbage collected (its a pain I know =/).
 
another method which I've not yet done but should work, is to create a new thread, create a lua reference to it (using lua_ref) then pop it off the stack immediately (so it doesn't get buried), and later when you are done with it, destroy the reference (lua_unref) and that should allow it to be GC'd
 
hope that helps,
 
Raymond Jacobs
Owner,
Ethereal Darkness Interactive

 
On 5/18/06, mos <[hidden email]> wrote:

Hi

in lua5.1 manual:

lua_newthread
         lua_State *lua_newthread (lua_State *L);
There is no explicit function to close or to destroy a thread. Threads are
subject to garbage collection, like any Lua object

       I don't understand this , for example like Npc in a game

       function AI()
               while(true) do
                       walkto(A)
                       walkto(B)
                       ...
                       walkto(Z)
               end
       end

       I bind each npc with a lua_State by lua_newthread

       but the npc will die and i should close the lua_State , but I can
not.
       what can I do ? or some better suggestion ?

Best Regards
mos



Reply | Threaded
Open this post in threaded view
|

RE: About lua_newthread

mos-4


>  
> another method which I've not yet done but should work, is to
> create a new thread, create a lua reference to it (using
> lua_ref) then pop it off the stack immediately (so it doesn't
> get buried), and later when you are done with it, destroy the
> reference (lua_unref) and that should allow it to be GC'd
>  

Hi!
        I dont know how to do and I just try like this:

        lua_State* m_L = lua_newthread(L);
        int refKey = luaL_ref(L, LUA_REGISTRYINDEX);

        NPC->L = L
        NPC->m_L = m_L
        NPC->refKey = refKey

        and
        NPC::~NPC(){
                if (L)
                {
                        luaL_unref(L,LUA_REGISTRYINDEX,refKey );
                }
        }
       
        no error occur, but I wonder if it will work as I expect.

mos


Reply | Threaded
Open this post in threaded view
|

Re: About lua_newthread

Raymond Jacobs
In reply to this post by Raymond Jacobs
I 'belive' that will work =)

On 5/19/06, mos <[hidden email]> wrote:


>
> another method which I've not yet done but should work, is to
> create a new thread, create a lua reference to it (using
> lua_ref) then pop it off the stack immediately (so it doesn't
> get buried), and later when you are done with it, destroy the
> reference (lua_unref) and that should allow it to be GC'd
>

Hi!
       I dont know how to do and I just try like this:

       lua_State* m_L = lua_newthread(L);
       int refKey = luaL_ref(L, LUA_REGISTRYINDEX);

       NPC->L = L
       NPC->m_L = m_L
       NPC->refKey = refKey

       and
       NPC::~NPC(){
               if (L)
               {
                       luaL_unref(L,LUA_REGISTRYINDEX,refKey );
               }
       }

       no error occur, but I wonder if it will work as I expect.

mos