小编ran*_*jak的帖子

多线程C Lua模块导致Lua脚本中的段错误


我为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;
}
Run Code Online (Sandbox Code Playgroud)


现在,如果我写一个非常简单的Lua脚本,那就是:

require("test")
test.start_mythread()
Run Code Online (Sandbox Code Playgroud)

运行脚本lua myscript.lua有时会导致段错误.以下是GDB对核心转储的看法:

Program terminated with …
Run Code Online (Sandbox Code Playgroud)

c linux lua multithreading pthreads

8
推荐指数
1
解决办法
992
查看次数

标签 统计

c ×1

linux ×1

lua ×1

multithreading ×1

pthreads ×1