Lua ::如何编写将加载多个CPU的简单程序? [英] Lua :: How to write simple program that will load multiple CPUs?

查看:109
本文介绍了Lua ::如何编写将加载多个CPU的简单程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在Lua中编写一个程序,该程序将加载多个CPU.由于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:

  • 在Lua中是不可能的
  • 我无法写it(希望是这种情况)

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

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... 为了演示Lua的权力.

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

谢谢...

推荐答案

在操作系统的意义上,Lua协程与线程不是同一回事.

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

OS线程是抢占式的.这意味着它们将在任意时间运行,并窃取操作系统规定的时间片.它们将在不同的处理器上运行(如果可用).它们可以在可能的同时运行.

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协程不这样做.协程可能具有线程"类型,但一次只能有一个活动的协程.协程将一直运行,直到协程本身通过发出coroutine.yield命令决定停止运行.一旦产生,它将不会再次运行,直到另一个例程对该特定的协程发出coroutine.resume命令.

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协程提供合作多线程,这就是为什么它们被称为 co <​​/strong>例程.他们彼此合作.一次只运行一件事,并且只有在任务明确要求这样做时,您才切换任务.

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.

您可能认为您可以只创建OS线程,在Lua中创建一些协程,然后在不同的OS线程中恢复每个线程.只要每个OS线程都在不同的Lua instance 中执行代码,此方法就起作用. Lua API是可重入的;您可以从不同的OS线程调用它,但是如果从不同的Lua实例调用,则只能 .如果您尝试通过同一个Lua实例进行多线程处理,则Lua可能会做不愉快的事情.

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.

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

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.

这篇关于Lua ::如何编写将加载多个CPU的简单程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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