Lua :: 如何编写加载多个CPU的简单程序?

发布于 2024-11-15 23:05:03 字数 388 浏览 2 评论 0原文

我还没有能够用 Lua 编写一个可以加载多个 CPU 的程序。由于Lua 通过协程支持这个概念,我相信这是可以实现的。

我失败的原因可能是以下之一:

  • 这在 Lua 中是不可能的
  • ,我无法编写它☺(并且我希望是这样的

有人可以更有经验吗(我两周前发现了 Lua)为我指明了正确的方向?


重点是编写一个在所有核心上进行高负载的数字运算脚本... 出于演示 Lua 威力的目的。

谢谢...

I haven't been able to write a program in Lua that will load more than one CPU. Since Lua supports the concept via coroutines, I believe it's achievable.

Reason for me failing can be one of:

  • It's not possible in Lua
  • I'm not able to write it ☺ (and I hope it's the case )

Can someone more experienced (I discovered Lua two weeks ago) point me in right direction?


The point is to write a number-crunching script that does hi-load on ALL cores...
For demonstrative purposes of power of Lua.

Thanks...

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

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

发布评论

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

评论(6

心如荒岛 2024-11-22 23:05:03

Lua 协程与操作系统意义上的线程不同。

操作系统线程是抢占式的。这意味着它们将在任意时间运行,窃取操作系统指定的时间片。如果可用,它们将在不同的处理器上运行。并且它们可以在可能的情况下同时运行。

Lua 协程不会这样做。协程可能具有“线程”类型,但一次只能有一个协程处于活动状态。协程将一直运行,直到协程本身通过发出 coroutine.yield 命令决定停止运行。一旦它屈服,它就不会再次运行,直到另一个例程向该特定协程发出 coroutine.resume 命令。

Lua 协程提供协作多线程,这就是它们被称为协程的原因。他们互相合作。一次仅运行一件事,并且仅当任务明确要求时才切换任务。

您可能认为您可以创建操作系统线程,在 Lua 中创建一些协程,然后在不同的操作系统线程中恢复每个协程。只要每个操作系统线程在不同的 Lua 实例中执行代码,这种方法就可以工作。 Lua API 是可重入的;您可以从不同的操作系统线程调用它,但如果是从不同的 Lua 实例调用。如果您尝试通过同一个 Lua 实例进行多线程,Lua 可能会做一些令人不快的事情。

所有现有的 Lua 线程模块都会为每个线程创建备用 Lua 实例。 Lua-lltreads 只是为创建一个全新的 Lua 实例每个线程;除了复制传递给新线程的参数之外,没有用于线程间通信的 API。 LuaLanes 确实提供了一些交叉连接代码。

Lua coroutines are not the same thing as threads in the operating system sense.

OS threads are preemptive. That means that they will run at arbitrary times, stealing timeslices as dictated by the OS. They will run on different processors if they are available. And they can run at the same time where possible.

Lua coroutines do not do this. Coroutines may have the type "thread", but there can only ever be a single coroutine active at once. A coroutine will run until the coroutine itself decides to stop running by issuing a coroutine.yield command. And once it yields, it will not run again until another routine issues a coroutine.resume command to that particular coroutine.

Lua coroutines provide cooperative multithreading, which is why they are called coroutines. They cooperate with each other. Only one thing runs at a time, and you only switch tasks when the tasks explicitly say to do so.

You might think that you could just create OS threads, create some coroutines in Lua, and then just resume each one in a different OS thread. This would work so long as each OS thread was executing code in a different Lua instance. The Lua API is reentrant; you are allowed to call into it from different OS threads, but only if are calling from different Lua instances. If you try to multithread through the same Lua instance, Lua will likely do unpleasant things.

All of the Lua threading modules that exist create alternate Lua instances for each thread. Lua-lltreads just makes an entirely new Lua instance for each thread; there is no API for thread-to-thread communication outside of copying parameters passed to the new thread. LuaLanes does provide some cross-connecting code.

得不到的就毁灭 2024-11-22 23:05:03

对于核心 Lua 库来说这是不可能的(如果你不计算创建多个进程并通过输入/输出进行通信),但我认为存在针对不同线程库的 Lua 绑定。

jpjacobs 的答案相关问题链接到LuaLanes,这似乎是一个多线程库。 (不过,我没有经验。)

如果您将 Lua 嵌入到应用程序中,您通常会希望将多线程以某种方式链接到应用程序的多线程。

It is not possible with the core Lua libraries (if you don't count creating multiple processes and communicating via input/output), but I think there are Lua bindings for different threading libraries out there.

The answer from jpjacobs to one of the related questions links to LuaLanes, which seems to be a multi-threading library. (I have no experience, though.)

If you embed Lua in an application, you will usually want to have the multithreading somehow linked to your applications multithreading.

梦晓ヶ微光ヅ倾城 2024-11-22 23:05:03

除了 LuaLanes 之外,还可以看看 llthreads

In addition to LuaLanes, take a look at llthreads

挽清梦 2024-11-22 23:05:03

除了已经建议的 LuaLanes、llthreads 和此处提到的其他内容之外,还有一种更简单的方法。

如果您使用的是 POSIX 系统,请尝试使用 posix.fork() 以老式方式执行此操作(来自 luaposix)。您知道,将任务分成批次,分叉与核心数量相同数量的进程,计算数字,整理结果。

另外,请确保您使用 LuaJIT 2 以获得最大速度。

In addition to already suggested LuaLanes, llthreads and other stuff mentioned here, there is a simpler way.

If you're on POSIX system, try doing it in old-fashioned way with posix.fork() (from luaposix). You know, split the task to batches, fork the same number of processes as the number of cores, crunch the numbers, collate results.

Also, make sure that you're using LuaJIT 2 to get the max speed.

情话难免假 2024-11-22 23:05:03

只需创建多个 Lua 解释器并在所有解释器中运行 lua 程序即可。

Lua 多线程是一种无共享模型。如果需要交换数据,则必须将数据序列化为字符串,并使用 ac 扩展或套接字或任何类型的 IPC 将它们从一个解释器传递到另一个解释器。

It's very easy just create multiple Lua interpreters and run lua programs inside all of them.

Lua multithreading is a shared nothing model. If you need to exchange data you must serialize the data into strings and pass them from one interpreter to the other with either a c extension or sockets or any kind of IPC.

泪冰清 2024-11-22 23:05:03

通过类似 IPC 的传输机制序列化数据并不是跨线程共享数据的唯一方法。

如果您使用面向对象的语言(例如 C++)进行编程,那么多个线程很可能通过对象指针跨线程访问共享对象,这样做是不安全的,除非您提供某种保证,保证没有两个线程会访问共享对象。尝试同时读取和写入相同的数据。

有很多选择可以实现这一点,无锁和无等待机制正变得越来越流行。

Serializing data via IPC-like transport mechanisms is not the only way to share data across threads.

If you're programming in an object-oriented language like C++ then it's quite possible for multiple threads to access shared objects across threads via object pointers, it's just not safe to do so, unless you provide some kind of guarantee that no two threads will attempt to simultaneously read and write to the same data.

There are many options for how you might do that, lock-free and wait-free mechanisms are becoming increasingly popular.

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