lua-users home
lua-l archive

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


Hello,

I am trying to use Lua script in a multi-threaded application. The
threads (pthreads) are created by a library that I use. For me it
looks like a new thread is created every second, then I must do
something  with the data I receive for this thread, and after that
I have to release all the data I created (though in fact I guess
the threads are reused)

So the problem is that if I want to use Lua scripts to do the job
in a thread safe manner, I need to create a new Lua universe for
each thread (that is every second), and compile the script (or at
least initilise and execute the chunk) every time (and also close
the Lua state when it is time to finish the thread). I am sure
this will work fine but I do not think it is the best solution.
I did not actually make any tests yet, but I do not like the idea
to do all this initialisation every time.

What I want is to load my script when the program starts, execute
it, perform some initialisation of the global environment (including
defining a few function which I will use in my threads), and then
just call lua_newthread for every new thread. The threads do not
need to cooperate in any way, they just need the ability to run
the functions that I defined and maybe read some other global
objects created on initialisation - NOT write them.

This is how I want to do this. 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. After that I create a brand new environment
table for the thread, create a new table `string' in it, create
references to the global string.find and other functions, do
the same thing with other libraries (including my own). Now this
new Lua thread has the environment which looks just like the initial
environment, but it cannot write to anything in it, so I am safe
from any mistakes in the script. To access the global data I may
create some simple getter functions.

After the thread is finished, I close the Lua thread, lock the mutex
again, destory the reference from the registry, and unlock the mutex.
Also I guess I have to lock it before reading the reference every
time (in case some other thread modify the registry at the same
moment). And I do not need any other locking.

Did I miss something, is this scheme correct? I really do not want
to redefine lua_lock, I do not need any communication between threads
except  creating the references in registry. But I am not sure about
garbage collector. Is it possible that two different pthreads will
try to garbage collect the same dereferenced Lua thread at the same
time? Any other problems?

Any advice is appreciated.

--
Regards, Sergey Redin.