[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Threads again
- From: Wim Couwenberg <w.couwenberg@...>
- Date: Sun, 29 Jan 2006 16:46:11 +0100
> I create one single mutex with
> pthread_mutex_init when the program starts. When a new thread is
> created and I need a new Lua thread for it, I lock the mutex,
> call lua_newthread, create the reference to this thread to prevent
> it from being garbage collected (and also to find my Lua thread in
> the next callback from my library), pop t from the main stack and
> unlock the mutex.
If I'm correct you use a single "administration state" to keep track
of all the states you create with lua_newthread. The separate states
are then driven by separate pthreads. This is not safe at all: if the
garbage collector runs in *any* of your states (including the
administration state) this garbage collect cycle visits *all* your
created states. So again, two or even more (p)threads are busy in the
same state simultaneously, which is very bad news. The administration
mutex will not protect you from this...
> After the thread is finished, I close the Lua thread, lock the mutex
Close the thread? Extra states created with lua_newthread cannot be
closed explicitly, only by a garbage collect cycle (possibly when the
administration state is closed.) Closing a thread and then removing
it from the registry doesn't help either, the garbage collector will
know of its existence anyway (obviously.) This will lead to double
destruction of your thread...
> Did I miss something, is this scheme correct?
I'd say it's not safe at all... If you don't want to use the
lua_lock/lua_unlock facility (and it's easy to see why you wouldn't)
there's not much else you can do than to create completely separate
lua universes with lua_open (or lua_newstate.)
--
Wim