lua-users home
lua-l archive

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


Some things to remember:

1.  lua_newthread(L) also leaves the created thread on the stack of L.  This
prevents it from being immediate collection by gc.  This also means that if
you're just dropping (ignoring) this stack slot, the thread will be happily
collected while you're still using it from C (by its lua_State pointer),
because indeed it is not reachable from the Lua root set.  So the lua thread
value is essential to keep around as well.

2.  Though called "threads" in the manual, this does *not* imply that you
can simply let a native preempting thread run it.  The Lua environment is
reentrant, but it is *not* thread safe!  So you should implement some thread
cooperation scheme.  There are several ways to go about, but each comes with
its own difficulties (i.e. overhead.)

    a.  Use hook functions to re-acquire a mutex every x lines/opcodes.
        (sort of preemptive cooperation!?  :-) )
    b.  Define the lua_lock and lua_unlock macro's to handle some mutex.
    c.  If you have high and low priority threads you might want to run the
        high priority ones uninterrupted, while the low priority threads
        release and re-acquire their mutex on request in a hook function.
    d.  ...

--
Wim