Lua/Luajit:暂停当前 Lua 线程
Lua/Luajit: pause current Lua thread
我目前正在使用Luajit和Lua 5.1,目前正在尝试在Lua C API中注册一个名为"Wait"的函数。该函数的主要目的是暂停当前线程。
用法示例:
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_pcall
不会启动可生成的协程。启动协程的正确函数是lua_resume
。
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- Lua/Luajit:暂停当前 Lua 线程
- Lua 关机双免费,关机期间 Lua 线程还活着?
- QT线程中的Lua脚本
- 从另一个线程中断c++中的lua脚本
- LuaPlus:如何从多线程C++中调用Lua函数
- 从不同的线程调用Lua函数(在C++中嵌入Lua)可以吗