相当于Lua coroutine.create在C ++中使用lua_newthread [英] Equivalent of Lua coroutine.create in C++ using lua_newthread
问题描述
我有一个回调系统,可以添加lua函数到C ++处理程序,例如。 in lua我可以做
I have a callback system, which can add lua functions to a C++ handler, e.g. in lua i can do
myCObject:AddCallback(luaFunc)
我也有同样的,对于协程
i also have the same, for coroutines
myCObject:AddCallback(coroutine.create(luaFunc))
然后我可以使用
lua_State * pThread = lua_tothread(L, -1);
lua_resume(pThread, 0,0);
lua函数。
现在,我不想要脚本编写者写coroutine.create(luaFunc) - 我只想自动转换一个lua func协程。当AddCallback被调用时,我有luaFunc堆栈 - 然后我如何继续? (使用coroutine.create我已经在堆栈上有一个线程)
Now, i dont want to require script writers to write coroutine.create(luaFunc) - and i just want to automatically "convert" a lua func to a coroutine. When AddCallback gets called, i have the luaFunc on the stack - and then how do i proceed? (with coroutine.create i already have a thread on the stack)
编辑:我寻找一个解决方案使用C API。 lua_newthread
Im looking for a solution that uses the C API e.g. lua_newthread
推荐答案
这个想法很简单。首先,创建一个新的线程。
The idea is fairly simple. First, you create a new thread.
lua_State *pThread = lua_newthread(L);
此函数还将该线程推送到 L
。下一步是让你的线程函数到 pThread
。鉴于此时在堆栈上有一个Lua函数,下一步是将该函数传递给 pThread
堆栈。
This function also pushes that thread onto L
. The next step is to get your thread function onto pThread
. Given that you have a Lua function on the stack at this point, your next step is to transfer that function to the pThread
stack.
有一个专门用于在线程之间传输值的函数: lua_xmove
。但是,它只传输堆栈的顶层元素。所以你需要将Lua函数从 L
的栈复制到 L
的顶部堆栈。然后 lua_xmove
它到新的堆栈。
There is a function specifically for transferring values between threads: lua_xmove
. However, it only transfers the top elements of the stack. So you need to copy the Lua function from where it is on L
's stack to the top of L
's stack. Then lua_xmove
it to the new stack.
lua_pushvalue(L, #); //Where # is the index in the stack where the function is.
//Remember that lua_newthread pushed a value on the stack, so compensate for that.
lua_xmove(L, pThread, 1); //Moves the function to the top of the new stack.
请记住 lua_xmove
moves 该值,从 L
中删除它。因此, lua_pushvalue
推送值,并且 lua_xmove
弹出它。因此,堆栈的顶部再次是 pThread
表示的 lua_State
。
Remember that lua_xmove
moves the value, which removes it from L
. So lua_pushvalue
pushes the value, and lua_xmove
pops it. So the top of the stack is again the lua_State
represented by pThread
.
之后,推送所有需要发送到函数的参数(显然为零),然后恢复。
After that, push all of the parameters you need to send to the function (which apparently is zero), and resume it.
lua_resume(pThread, 0);
总代码:
lua_State *pThread = lua_newthread(L);
lua_pushvalue(L, #); //Where # is the index in the stack where the function is.
//Remember that lua_newthread pushed a value on the stack, so compensate for that.
lua_xmove(L, pThread, 1); //Moves the function to the top of the new stack.
lua_resume(pThread, 0);
一个Lua线程C API)是一个Lua值,就像一个表,userdata,string等等。因此,它是受垃圾收集。
A Lua thread (whether created in Lua or in the C API) is a Lua value, just like a table, userdata, string, etc. Therefore, it is subject to garbage collection. It will be collected when Lua detects that there are no more references to the value.
请记住: lua_newthread
将线程到原始堆栈。它是由你把它复制到注册表,或全球环境或任何你打算永久居住的线程。只要保留指向 lua_State
的指针,它就不会确保线程仍然有效。
Remember: lua_newthread
pushes the thread onto the original stack. It is up to you to copy it into the registry, or into the global environment or wherever you intend for that thread to permanently reside. Simply keeping a pointer to the lua_State
that it generated will not ensure that the thread remains alive.
这篇关于相当于Lua coroutine.create在C ++中使用lua_newthread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!