Reputation: 163
From my lua state L1, I call my registered c function makethread.
static int makethread(lua_State *L1) {
printf("makethread!\n");
pthread_t thread2;
pthread_create( &thread2,NULL,dumb_thread,NULL);
printf("makethread complete!\n");
return 0;
}
which attempts to run this dumb_thread
void * dumb_thread() {
printf("dumb thread!\n");
lua_State * L2;
L2= luaL_newstate();
lua_close(L2);
printf("dumb thread complete!\n");
return 0;
}
It looks like the program finished, but the program freezes because of the lua_close. All print statements fire, but I never get control of my lua terminal again. Also, while it says makethread completes, further code in my L1 lua state doesn't run. To me this indicates that lua is hung up trying to close L2. If I comment out lua_close, everything is fine even with the memory leak.
makethread!
makethread complete!
dumb thread!
dumb thread complete!
But if I call dumb_thread directly from my L1 state,
static int calldirectly(lua_State *L1) {
dumb_thread()
return 0;
}
Everything works as expected and I have access to my lua terminal. Further code in lua L1 works.
What can I do to make this multithreading work?
Upvotes: 3
Views: 210
Reputation: 163
I neglected to mention our locking system. Turns out our modified lua library (liblua.so) uses a global locking variable. Calling lua_close naturally unlocks this variable. However, both L1 and L2 states were sharing the same lock.
The solution is to simply create separate locking variables for each lua state.
Oops...
Upvotes: 2