【问题标题】:A core-dump when using lua_yield and lua_resume使用 lua_yield 和 lua_resume 时的核心转储
【发布时间】:2013-01-11 06:46:01
【问题描述】:

我只想恢复 func 协程两次,如果 n==0 则返回,如果 n==1 则返回,但是它核心转储,有什么问题吗?

“hello world”应该始终留在 LL 的堆栈中,我不知道出了什么问题。

[liangdong@cq01-clientbe-code00.vm.baidu.com lua]$ ./main 
func_top=1 top=hello world
first_top=1 top_string=hello world
Segmentation fault (core dumped)
[liangdong@cq01-clientbe-code00.vm.baidu.com lua]$ cat main.c 
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

int n = 0;

int func(lua_State *L) {
    printf("func_top=%d top=%s\n", lua_gettop(L), lua_tostring(L, -1));
    if (!n) {
        ++ n;
        return lua_yield(L, 1);
    } else {
        return 1;
    }
}

int main(int argc, char* const argv[]) {
    lua_State *L = luaL_newstate();

    /* init lua library */    
    lua_pushcfunction(L, luaopen_base);
    if (lua_pcall(L, 0, 0, 0) != 0) {
        return 1;
    }
    lua_pushcfunction(L, luaopen_package);
    if (lua_pcall(L, 0, 0, 0 ) != 0) {
        return 2;
    }

    /* create the coroutine */
    lua_State *LL = lua_newthread(L);

    lua_pushcfunction(LL, func);
    lua_pushstring(LL, "hello world");

    /* first time resume */
    if (lua_resume(LL, 1) == LUA_YIELD) {
        printf("first_top=%d top_string=%s\n", lua_gettop(LL), lua_tostring(LL, -1));
        /* twice resume */
        if (lua_resume(LL, 1) == 0) {
            printf("second_top=%d top_string=%s\n", lua_gettop(LL), lua_tostring(LL, -1));
        }
    }

    lua_close(L);

    return 0;
}

它在 lua5.1 中进行核心转储,但如果将 lua_resume(LL, 1) 更改为 lua_resume(LL, NULL, 1),则在 lua5.2 中运行良好。

【问题讨论】:

  • 你能用gdb给一个回溯吗?

标签: lua coroutine


【解决方案1】:

编辑:我实际上完全错了。

You cannot resume a C function.

【讨论】:

    猜你喜欢
    • 2011-07-07
    • 2015-11-13
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 2015-11-18
    • 1970-01-01
    • 2015-05-12
    相关资源
    最近更新 更多