I have two C threads, and two lua_States (created via lua_newthread(mMainLuaState)). I have a function setup that ensures correct data parallelism, so in theory thread1 and thread2 should be able to both call the function at the same time (the two calls have their own environment, and only read static read only globals). However, I am getting one crash after another (no helpful information provided in the logs sorry).
Check out my original thread for more details and example code...
http://forum.luahub.com/index.php?topic=2596.0
There is no way I am the first person to try this... somebody out there must know what I am 'missing'?
*fyi I have NOT defined lua_lock/unlock, but for what I am trying to do, I should not have to right?
*here is the example code to save you from having to check out the other thread...
Lua
-- Create a unique message buffer for each threadmsgs2 = {}msgs3 = {}-- Declare two unique functions for each threadfunction foo2() table.insert(msgs2, 777); endfunction foo3() table.insert(msgs3, 777); end
C
mBarrier = new boost::barrier(3); // The main lua state is initialize (mLua), create two 'sub states' mLua2 = lua_newthread(mLua); assert(lua_type(mLua, -1) == LUA_TTHREAD); int lua2Ref = luaL_ref(mLua, LUA_REGISTRYINDEX); assert(lua_type(mLua, -1) != LUA_TTHREAD); mLua3 = lua_newthread(mLua); assert(lua_type(mLua, -1) == LUA_TTHREAD); int lua3Ref = luaL_ref(mLua, LUA_REGISTRYINDEX); assert(lua_type(mLua, -1) != LUA_TTHREAD); // Spawn two threads which use mLua2/mLua3 to call foo2/foo3 boost::thread thread2(boost::bind(&Sim::lua2Task, this)); boost::thread thread3(boost::bind(&Sim::lua3Task, this)); mBarrier->wait();
void call(lua_State* lua, std::string& func){ lua_getglobal(lua, func.c_str()); assert(lua_type(lua, -1) == LUA_TFUNCTION); if (lua_pcall(lua, 0, 0, 0)) Aux::log(lua_tostring(lua, -1));}void Sim::lua2Task(void){ for (int i = 0; i < 10000; ++i) call(mLua2, std::string("foo2")); mBarrier->wait();}void Sim::lua3Task(void){ for (int i = 0; i < 10000; ++i) call(mLua3, std::string("foo3")); mBarrier->wait();}