【发布时间】:2015-02-17 18:50:15
【问题描述】:
我为 Lua 编写了一个非常简单的 C 库,它由一个启动线程的函数组成,所述线程除了循环之外什么都不做:
#include "lua.h"
#include "lauxlib.h"
#include <pthread.h>
#include <stdio.h>
pthread_t handle;
void* mythread(void* args)
{
printf("In the thread !\n");
while(1);
pthread_exit(NULL);
}
int start_mythread()
{
return pthread_create(&handle, NULL, mythread, NULL);
}
int start_mythread_lua(lua_State* L)
{
lua_pushnumber(L, start_mythread());
return 1;
}
static const luaL_Reg testlib[] = {
{"start_mythread", start_mythread_lua},
{NULL, NULL}
};
int luaopen_test(lua_State* L)
{
/*
//for lua 5.2
luaL_newlib(L, testlib);
lua_setglobal(L, "test");
*/
luaL_register(L, "test", testlib);
return 1;
}
现在,如果我编写一个非常简单的 Lua 脚本就可以了:
require("test")
test.start_mythread()
使用lua myscript.lua 运行脚本有时会导致段错误。以下是 GDB 对核心转储的评价:
Program terminated with signal 11, Segmentation fault.
#0 0xb778b75c in ?? ()
(gdb) thread apply all bt
Thread 2 (Thread 0xb751c940 (LWP 29078)):
#0 0xb75b3715 in _int_free () at malloc.c:4087
#1 0x08058ab9 in l_alloc ()
#2 0x080513a2 in luaM_realloc_ ()
#3 0x0805047b in sweeplist ()
#4 0x080510ef in luaC_freeall ()
#5 0x080545db in close_state ()
#6 0x0804acba in main () at lua.c:389
Thread 1 (Thread 0xb74efb40 (LWP 29080)):
#0 0xb778b75c in ?? ()
#1 0xb74f6efb in start_thread () from /lib/i386-linux-gnu/i686/cmov/libpthread.so.0
#2 0xb7629dfe in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:129
主线程的堆栈不时有一些变化。
似乎 start_thread 函数想要跳转到有时恰好属于无法访问的内存的给定地址(在本例中为 b778b75c)。
编辑
我也有一个 valgrind 输出:
==642== Memcheck, a memory error detector
==642== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==642== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==642== Command: lua5.1 go.lua
==642==
In the thread !
In the thread !
==642== Thread 2:
==642== Jump to the invalid address stated on the next line
==642== at 0x403677C: ???
==642== by 0x46BEEFA: start_thread (pthread_create.c:309)
==642== by 0x41C1DFD: clone (clone.S:129)
==642== Address 0x403677c is not stack'd, malloc'd or (recently) free'd
==642==
==642==
==642== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==642== Access not within mapped region at address 0x403677C
==642== at 0x403677C: ???
==642== by 0x46BEEFA: start_thread (pthread_create.c:309)
==642== by 0x41C1DFD: clone (clone.S:129)
==642== If you believe this happened as a result of a stack
==642== overflow in your program's main thread (unlikely but
==642== possible), you can try to increase the size of the
==642== main thread stack using the --main-stacksize= flag.
==642== The main thread stack size used in this run was 8388608.
==642==
==642== HEAP SUMMARY:
==642== in use at exit: 1,296 bytes in 6 blocks
==642== total heap usage: 515 allocs, 509 frees, 31,750 bytes allocated
==642==
==642== LEAK SUMMARY:
==642== definitely lost: 0 bytes in 0 blocks
==642== indirectly lost: 0 bytes in 0 blocks
==642== possibly lost: 136 bytes in 1 blocks
==642== still reachable: 1,160 bytes in 5 blocks
==642== suppressed: 0 bytes in 0 blocks
==642== Rerun with --leak-check=full to see details of leaked memory
==642==
==642== For counts of detected and suppressed errors, rerun with: -v
==642== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Killed
但是,到目前为止,我一直很好,只需打开 lua 解释器并一个接一个地手动输入相同的指令。
另外,一个 C 程序做同样的事情,使用相同的库:
int start_mythread();
int main()
{
int ret = start_mythread();
return ret;
}
应该如此,在我的测试中从未失败过。
我已经尝试过 Lua 5.1 和 5.2,但无济于事。
编辑: 我应该指出我在运行 32 位 Debian Wheezy (Linux 3.2) 的单核 eeePC 上测试了这个。
我刚刚在我的主机(4 核 64 位 Arch linux)上再次进行了测试,并使用lua myscript.lua segfaults 每次启动脚本...
从解释器提示符输入命令可以正常工作,以及上面的 C 程序。
我首先编写这个小库的原因是因为我正在编写一个更大的库,我首先遇到了这个问题。经过数小时徒劳的调试,包括一个一个地删除每个共享结构/变量(是的,我是那么绝望),我已经归结为这段代码。
所以,我的猜测是我在 Lua 上做错了什么,但那可能是什么?我已经尽可能多地搜索了这个问题,但我发现大多数人在从多个线程中使用 Lua API 时遇到问题(这不是我在这里想要做的)。
如果您有任何想法,我们将不胜感激。
编辑
更准确地说,我想知道在编写用于 Lua 脚本的 C 库时是否应该对线程采取额外的预防措施。 Lua 需要在动态加载库中创建的线程在“卸载”库时终止吗?
【问题讨论】:
-
另外,当我将
mythread创建为分离时,我会得到相同的行为。 -
我认为 rpattiso 的回答涵盖了它。根据 pthread 文档,您不希望在子线程仍在运行时主线程退出的情况。现在您是否必须按照您的建议进行模块卸载?不,但实际上,这可能是最直接的。你可以做的就是把它连接到一个
__gc上,pthread_exit被调用。将其连接到一个虚拟的全局对象,该对象会一直存在直到您退出 vm。这样可以确保在主退出之前调用pthread_exit。
标签: c linux multithreading lua pthreads