lua-users home
lua-l archive

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


> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Laurent FAILLIE
> Sent: zaterdag 6 juni 2015 18:51
> To: Lua mailing list
> Subject: Re: Need some help to use TFUNCTION
> 
> Hello,
> 
> 
> I have another question related to Lua's stat which is expected to be passed
> as 1st arg for Lua's function : is it thread safe ?
> 
> My application handles MQTT messages in asynchronous mode meaning messages
> handling (and consequently the Lua callbacks I want to call for every
> arriving messages) is running in a separate thread ... but it is safe to use
> the same lua_stat ? what will happen if a message arrive whereas the main
> thread is already running a Lua function ?
> 
> Thanks
> 
> Laurent
> 
> 

The naming of some elements in the C api is a bit misleading. Personally I wish that all wording with "thread" would be changed to "coroutine", and the lua_State would be renamed to "executionstack" or something more appropriate.

Have a look here [1] for some "definitions" I came up with when I started with multi-threading and wrote my DarkSideSync library. To deal with multithreading there are a number of libraries that handle things for Lua
- Lanes [2] (create new states and threads from Lua)
- Rings [3] (lighter than Lanes, but also lesser capabilities)
- llthreads [4]
- darksidesync [5] (a helper library to synchronise background tasks/os-threads back to a single Lua state)
- ZMQ [6] (or equivalents) to communicate between multiple states (to work around lacking shared global data)

I'm not familiar with Paho, but how is is used? Is the main loop on the C side and are messages passed to Lua to be handled with scripting, or is the main loop on the Lua side? Does Paho create the new os threads? Do you have control over that behaviour? Does it also generate new Lua states?


Thijs

[1] http://www.thijsschreijer.nl/blog/?p=693
[2] http://cmr.github.io/lanes/ 
[3] http://keplerproject.github.io/rings/ 
[4] https://github.com/Neopallium/lua-llthreads 
[5] https://github.com/Tieske/DarkSideSync 
[6] http://zeromq.org/bindings:lua