我目前正在Lua 5.1中使用Luajit,目前正在嘗試在Lua C API中註冊一個名爲「Wait」的函數。該函數的主要目的是暫停當前線程。Lua/Luajit:暫停當前Lua線程
用法示例:
print("Working");
Wait()
print("A");
但是該功能不正常工作。這是我的C++代碼。
#include <iostream>
#include <Windows.h>
extern "C" {
#include "Luajit/luajit.h"
#include <Luajit/lua.h>
#include <Luajit/lualib.h>
#include <Luajit/lauxlib.h>
}
static int wait(lua_State* lua) {
return lua_yield(lua, 0);
}
int main() {
lua_State* lua = luaL_newstate();
if (!lua) {
std::cout << "Failed to create Lua state" << std::endl;
system("PAUSE");
return -1;
}
luaL_openlibs(lua);
lua_register(lua, "Wait", wait);
lua_State* thread = lua_newthread(lua);
if (!thread) {
std::cout << "Failed to create Lua thread" << std::endl;
system("PAUSE");
return -1;
}
int status = luaL_loadfile(thread, "Z:/Projects/Visual Studio/Examples/Lua/Debug/Main.lua");
if (status == LUA_ERRFILE) {
std::cout << "Failed to load file" << std::endl;
system("PAUSE");
return -1;
}
int error = lua_pcall(thread, 0, 0, 0);
if (error) {
std::cout << "Error: " << lua_tostring(thread, 1) << std::endl;
}
system("PAUSE");
return 0;
}
當我加載了Lua中我張貼上面我得到以下輸出:
Working
Error: attempt to yield across C-call boundary
Press any key to continue . . .
我一直在Lua編程現在超過4年。我剛剛開始使用C API,而且我從未見過C調用邊界錯誤。我做了一些谷歌搜索和問朋友,似乎沒有人能夠幫助我。任何想法我的代碼有什麼問題?
當我在C++中調用lua_yield(lua,0)函數時發生錯誤。
我試了下面的問題的答案,似乎沒有任何工作。
http://stackoverflow.com/questions/8459459/lua-coroutine-error-tempt-to-yield-across-metamethod-c-call-boundary
[Lua協程錯誤:試圖通過元方法/ C調用邊界產生的誘惑]的可能重複(http://stackoverflow.com/questions/8459459/lua-coroutine-error-tempt-to-yield-across-metamethod -c-call-boundary) – Skewled
不,那個答案沒有幫助。 – Kurieita