从我的 lua 状态 L1,我调用我注册的 c 函数 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;
}
试图运行这个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;
}
看起来程序完成了,但是由于 lua_close,程序冻结了。所有打印语句都会触发,但我再也无法控制我的 lua 终端。此外,虽然它说 makethread 完成,但我的 L1 lua 状态下的进一步代码没有运行。对我来说,这表明 lua 在尝试关闭 L2 时挂断了。如果我注释掉 lua_close,即使内存泄漏,一切都很好。
makethread!
makethread complete!
dumb thread!
dumb thread complete!
但是如果我直接从我的 L1 状态调用dumb_thread,
static int calldirectly(lua_State *L1) {
dumb_thread()
return 0;
}
一切都按预期工作,我可以访问我的 lua 终端。lua L1 中的进一步代码有效。
我该怎么做才能使这个多线程工作?