LuaInterface多线程崩溃

发布于 2024-12-15 14:45:40 字数 455 浏览 4 评论 0原文

有没有办法让LuaInterface在多线程环境下工作?

我有一个多线程 C# (.Net 4) 程序集,它使用 LuaInterface 处理来自本机应用程序的数据。每个线程都有自己的 Lua 解释器实例。我使用 Lua.GetFunction() 从脚本中检索函数并定期调用该函数。我将字典传递给函数进行处理。这对于一个线程来说工作得很好。但是,当我使用两个线程时,它会使整个应用程序崩溃,并且我在 Visual Studio 中看到如下错误:

The thread 'Win32 Thread' (0xa78) has exited with code -1073740791 (0xc0000409).

如果我更改脚本以执行一些微不足道的操作,其中它不使用我传递给它的字典,那么它也可以在多个线程中正常工作。

我是否必须为每个解释器提供自己的进程或 AppDomain 才能完成这项工作?

Is there a way to make LuaInterface work in a multithreaded environment?

I have a multithreaded c# (.Net 4) assembly that uses LuaInterface to process data from a native application. Each thread has its own instance of the Lua interpreter. I use Lua.GetFunction() to retrieve a function from a script and call that function periodically. I pass a Dictionary to the function to process. This works fine with one thread. But when I use two threads it crashes the entire app and I see errors like the following in visual studio:

The thread 'Win32 Thread' (0xa78) has exited with code -1073740791 (0xc0000409).

If I change the script to do something trivial where it does not utilize the Dictionary I pass to it then it also works fine with multiple threads.

Am I going to have to give each interpreter its own process or AppDomain to make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秉烛思 2024-12-22 14:45:40

如果您尝试从两个不同的线程调用同一个 Dictionary 对象,则会出现数据争用。无论是因为 Lua 脚本这么说还是因为 C# 代码试图这样做,都没有关系。这仍然是一个竞争条件。如果该字典不是线程安全的,则可能会导致错误。

因此,您要么需要为此对象提供线程安全的访问器,要么不需要从两个线程访问同一个对象。这实际上与 Lua 没有任何关系;这只是基本的多线程。

If you are trying to call the same Dictionary object from two different threads, then you have a data race. It doesn't matter if it's doing it because a Lua script said so or because C# code tried to do it. It's still a race condition. And if that Dictionary is not thread-safe, badness can result.

So you either need to provide thread-safe accessors to this object, or you need to not access the same object from two threads. This doesn't really have anything to do with Lua; it's just basic multithreading.

谁人与我共长歌 2024-12-22 14:45:40

LuaInterface 不是线程安全的。据我所知,Lua 本身似乎支持多线程(参见 Lua Lanes)。不过,LuaInterface(v2.0.3.7) 仍然存在一些问题 在线程安全之前先算出来。将 Lua 解释器的单独实例放在自己的线程中并不能解决这些问题。

LuaInterface is not thread safe. From what I have read Lua itself appears to support multithreading (see Lua Lanes). However, LuaInterface(v2.0.3.7) still has some issues to work out before it is thread safe. Putting separate instances of the Lua interpreter in their own thread does not overcome these issues.

陈年往事 2024-12-22 14:45:40

脚本对字典做了什么?这是相关的,因为字典类不是线程安全的。

比如说,如果字典在一个线程中被更改,而另一个线程枚举它,则该线程将崩溃。

What is the script doing with the dictionary? This is relevant since the dictionary class is not thread-safe.

Say, if the dictionary was changed in one thread while another thread enumerates it, that thread will crash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文