problem lua_yield and lua_resume

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

problem lua_yield and lua_resume

mos-4

Hi!
        I am very sorry to paste the full code -- or I can not explain the
whole thing clearly
        the code will use lua5.1 luabind0.7 , if you do not use luabind0.7 ,
it doesn't matter, you may help me also

        the source code of c++

extern "C" {
        #include "lua.h"
        #include "lualib.h"
        #include "lauxlib.h"
}

#include <luabind/luabind.hpp>
#include <luabind/error.hpp>
#include <string>
#include <stdio.h>
#include <iostream>
#include <sstream>

using namespace luabind;
using namespace std;

lua_State *L2;
string swait;
void MyYield(const char*szWait)
{
        cout << "thread is waiting for " << szWait << endl;
        swait = szWait;
        lua_yield(L2,0);
}

int luaMyYield(lua_State* LL)
{
        MyYield(lua_tostring(LL,1));
        return 0;
}

void RegisterLua()
{
        lua_register(L2,"MyYield",luaMyYield);
}

void RegisterLuabind()
{
        module(L2)
        [
                def("MyYield", MyYield)
        ];
}

int main()
{
        lua_State* L = lua_open();
        luabind::open(L);
        luaL_openlibs(L);

        L2 = lua_newthread (L);

        RegisterLuabind(); // ******************************
        //RegisterLua(); // ******************************

        int error = luaL_dofile(L2,"hello.lua");
        if (error != 0)
        {
                if(lua_isstring(L2, -1))
                {
                        const char* err = lua_tostring(L2, -1);
                        cout << err << endl;
                }
                return -1;
        }
        lua_getglobal(L2, "mythread");
        lua_resume(L2,0);

        while(1)
        {
                cout << "please input ... " << endl;
                string s ;
                cin >> s;
                if (s == swait)
                {
                        cout << "in main input " << s << endl;
                        lua_resume(L2,0);
                        if (s == "quit")
                                break;
                }
                else
                {
                        cout << "not match input" << endl;
                }
        }
}

        and the hello.lua file

function mythread()
        print "need yield"
        MyYield("yield")
        print "need quit"
        MyYield("quit")
end


        it works well as:

        need yield
        thread is waiting for yield
        please input ...
        yield  // keyboard input
        in main input yield
        need quit
        thread is waiting for quit
        please input...
        quit   // keyboard input

        all are ok!
        but if  comment RegisterLuabind uncomment RegisterLua

        //RegisterLuabind(); // ******************************
        RegisterLua(); // ******************************
       
        need yield
        thread is waiting for yield
        //skip the pre input...
        need quit
        thread is waiting for quit
        please input...
        quit   // keyboard input
        it crashed

        why?
        I had thought the RegisterLuabind and RegisterLua are the same
thing, but there are some magic beyond what I know :(

mos


Reply | Threaded
Open this post in threaded view
|

Re: problem lua_yield and lua_resume

mos-4

 
Hi
        sorry . it is my fault after I read the 5.1 manal

       
lua_yield
          int lua_yield  (lua_State *L, int nresults);

Yields a coroutine.

This function should only be called as the return expression of a C
function, as follows:

       return lua_yield (L, nresults);

        so modify the code like

//void MyYield(const char*szWait)
//{
// cout << "thread is waiting for " << szWait << endl;
// swait = szWait;
// lua_yield(L2,0);
//}

//int luaMyYield(lua_State* LL)
//{
// MyYield(lua_tostring(LL,1));
// return 0;
//}

int MyYield(const char*szWait)
{
        cout << "thread is waiting for " << szWait << endl;
        swait = szWait;
        return lua_yield(L2,0);
}

int luaMyYield(lua_State* LL)
{
        return MyYield(lua_tostring(LL,1));
}

        but after do it , the lua works ok but luabind crashed !
        my God !