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 <josephcsible@gmail.com> wrote:
On Sat, Jun 13, 2020 at 2:05 PM James Pascoe <james@james-pascoe.com> 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].

[1]: https://lualanes.github.io/lanes/
[2]: https://github.com/askyrme/luaproc/

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: https://github.com/wahern/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: http://25thandclement.com/~william/projects/cqueues.html

There is also a web library called lua-http built on cqueues for very high performance web functionality: https://github.com/daurnimator/lua-http

Regards,
Russell


Joseph C. Sible00