[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: problem lua_yield and lua_resume
- From: "mos" <mmosquito@...>
- Date: Sun, 30 Apr 2006 23:31:28 +0800
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