lua-users home
lua-l archive

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


Hi,

On 16.06.20 00:23, Andrea wrote:
> I have some question highlight below
>       o *Why one would need mtstates/mtmsg found at  https://github.com/osch?*

The packages mtmsg/mtstates/mtint are only small building blocks to build up
higher level multi threading solutions in Lua scripting language on top of them.

All the other libraries you are mentioning are all-in-one solutions that are
trying to solve several tasks at once in one bigger library/framework.

I started my first multi threading experiments using LuaLanes. However I wanted
to have something more basic. Then I discovered llthreads2 [1] which does only
provide the bare minimum for starting threads, nothing else. Even no inter
thread communication. You could e.g. use ZeroMQ [2] or something else for inter
thread communication with llthreads.

mtmsg [3] provides simple inter thread communication that does not rely on any
thirdparty lib and can be used with arbitrary lua threading libraries. It's main
purpose is to be an as low level as possible companion for llthreads2. But you
can use it in arbitrary scenarions as well: e.g. if you have two bigger parts in
you application that are using different multi threading frameworks/libraries
you can still use mtmsg for inter thread communication between the parts.

mtstates [4] is something I haven't found in other libraries: it is similar to
Rings [5], but allows the created Lua states to be passed to other threads, i.e.
you can invoke a function in the state from different threads, but only from one
at a time. A mtstate object is therefore an object together with a mutex (if you
know Java: this is like having an object with synchronized methods). Again this
library is only an as low level as possible solution that can be used together
with other multi threading solutions to build up higher level constructs.

You could use mtstates to build some sort of simple interthread communication,
but this would be overkill. A typical use case for mtstates is to build up some
sort of framework that is distributing a lot of coroutines on different threads
(for the case that you have a lot of more coroutines than hardware threads). For
this you would put each coroutine into one mtstate object (mtstates object are
lightweight like Lua states). This means you could build something similiar like
cqueues [6] but using more than one system thread.

mtint [7] is also as low level as possible and provides a solution for
interrupting threads. Before writing this, I created a solution for this just
for llthread2 [8] but then I discovered that a simple library could be build
just to interrupt any Lua thread independently from the used multi threading
library.


[1]: https://luarocks.org/modules/moteus/lua-llthreads2
[2]: https://github.com/Neopallium/lua-zmq
[3]: https://luarocks.org/modules/osch/mtmsg
[4]: https://luarocks.org/modules/osch/mtstates
[5]: https://keplerproject.github.io/rings/
[6]: https://github.com/wahern/cqueues
[7]: https://luarocks.org/modules/osch/mtint
[8]: https://github.com/moteus/lua-llthreads2/pull/16