[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Why one would need mtstates/mtmsg?
- From: Oliver <oschmidt-mailinglists@...>
- Date: Tue, 16 Jun 2020 10:29:57 +0200
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  which does only
provide the bare minimum for starting threads, nothing else. Even no inter
thread communication. You could e.g. use ZeroMQ  or something else for inter
thread communication with llthreads.
mtmsg  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  is something I haven't found in other libraries: it is similar to
Rings , 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  but using more than one system thread.
mtint  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  but then I discovered that a simple library could be build
just to interrupt any Lua thread independently from the used multi threading