lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Sat, Jun 13, 2020 at 11:17 AM Joseph C. Sible <> wrote:
On Sat, Jun 13, 2020 at 2:05 PM James Pascoe <> wrote:
> Also, the reference manual states that Lua threads created by lua_newthread() share the same global environment, which (as I understand it) means that they point to the same global environment in the parent lua_State. However, what I am trying to understand is whether it is possible to run individual Lua threads in separate OS threads safely?

No, that's not possible to safely do with stock Lua. Different OS
threads running Lua at the same time can only safely use completely
separate states, not threads that share a global state.

> And also, is it possible for Lua threads running in separate OS threads to communicate?

On their own, separate Lua states can't communicate at all. You'd have
to use some kind of helper to communicate between them, like Lua Lanes
[1] or luaproc [2].


If you are working in a unix-like environment, you can also flip the threading model on it's head and use an event loop/engine like cqueues: Cqueues are easy to use because they rely on co-routines and an OS event library (which is transparent to you). There is no callback model so processes are just continuous loops. The documentation describes how it works:

There is also a web library called lua-http built on cqueues for very high performance web functionality:


Joseph C. Sible00