lua-users home
lua-l archive

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

I'll lay out the gory details below, but here's a summary of the problem:

I'm creating two threads (in Java) in an Android app. Each thread has its own Lua state and each state is running its own copy of the same simple script. The script runs a for loop which prints out a log message, sleeps and repeats. The sleep is implemented by a simple wrapper that calls the C usleep function. Usleep, according to its documentation, suspends the thread that invokes it.

I would expect that the log messages from the two threads should be intermingled. Instead, I get all the log messages from the first thread followed by all the log messages from the second thread. By adjusting the thread priorities, I can get all the second thread's messages before the first thread. But I cannot get them to intermingle.

A few points to note:

- I am using a severely truncated implementation of LuaJava that I have mangled to use Lua 4.0.1 - if I run this experiment with both threads implemented in Java, it behaves as I expect - if I run this experiment with one thread running my Lua script and the second thread implmented in Java, it behaves as I expect - I have verified that the two separate Lua states are, in fact, two separate Lua states; i.e. I am not accidentally referring to the same state in both threads (see below for how I did this)

Anyway I would appreciate any suggestions, pointing out obvious things I've overlooked, pointing out subtle things that I won't be embarassed to have overlooked, etc.



Implementation details.

I verified that the two Lua states were actually different Lua states as follows: first, I checked the two Lua states to make sure were they were not the same Java object. Then, I tried to rule out any weirdness with the C functions by defining three sets of variables (one set defined only in thread 1, one set defined only in thread 2, and one set defined in both) then I had each state print out all the variables, the ones that weren't defined were nil, and the ones that were defined printed out their expected values

Here's the Lua script. 'androidlog' is a wrapper around __android_log_print from the NDK. 'threadname' is passed in from Java and is different for each thread.

       for k = 1,20 do
         androidlog(threadname, 'Greetings from thread ' .. threadname)

Here's an excerpt from the app's main activity where I create and start the threads.

       t1 = new LuaThread("loop.lua", getApplicationContext());
       t1.preload("threadname = 't1'");
       t2 = new LuaThread("loop.lua", getApplicationContext());
       t2.preload("threadname = 't2'");


LuaThread is a subclass of Thread. The constructor creates a Lua state and stores it in an instance variable. The preload function invokes doString on the object's Lua state with the passed in parameter. The start method kicks off the object's run method as a separate thread. Run basically does doFile with the file name passed in to the object's constructor.