Lua :: How to write simple program that will load multiple CPUs?

DinGODzilla picture DinGODzilla · Jun 17, 2011 · Viewed 15.3k times · Source

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...

Answer

Nicol Bolas picture Nicol Bolas · Jun 17, 2011

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.