lua_newstate和lua_newthread [英] lua_newstate vs lua_newthread

查看:1369
本文介绍了lua_newstate和lua_newthread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我现有的多线程应用程序中实现lua。我听说lua不是线程安全的我已经创建不同的 lua_State(s)不同的线程。

I am trying to implement lua into my existing multi threaded application. I heard that lua was not thread safe I have been creating different lua_State(s) for different threads.

通过lua头文件我发现了 lua_newthread 。你如何将它实现到一个全部准备好的线程应用程序。您是否创建一个 lua_State 并创建单独的 lua_newthread(s),否则会导致其他问题?

Looking through the lua header files I found lua_newthread. How would you implement that into an all ready threaded application. Do you create one lua_State and create separate lua_newthread(s) or would that lead to other issues?

推荐答案

注意,Lua线程不是操作系统线程(尽管有误导性的名称),只有Lua本身(不在主机应用程序中)以异步方式执行代码。

Watch out, Lua threads are not operating system threads (despite the misleading name), only facilities in Lua itself (not in the host application) to execute code in an asynchronous fashion.

因此答案是:create一个 lua_State 您的应用程序中的每个线程,如果您需要在状态之间传递数据,则使用序列化库作为中间件。

So the answer is: create one lua_State per thread in your application, and use a serialization library as middleware if you need to pass data between the states.

这篇关于lua_newstate和lua_newthread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆