lua-users home
lua-l archive

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


On 27.03.2018 17:07, Laurent FAILLIE wrote:

Hello,

 

I’m thinking to migrate my very own home automation tools from 5.1 to 5.3 …

One of the biggest problem I’m facing with Lua is multi-threading : all these tools are events driven, especially,  each and every MQTT message arrival triggers a new thread running some Lua code, and this is driven from C side.

 On 5.3 documentation, I can read

Each Lua state has one or more threads, which correspond to independent, cooperative lines of execution. The type lua_State (despite its name) refers to a thread. (Indirectly, through the thread, it also refers to the Lua state associated to the thread.)” but cooperative looks to me it still not real preemptive multi-threading but still co-routines like, isn’t it ?

 In my current application (using Lua 5.1), I’m creating new State for each thread but it create some issues :

*         Global variables can’t be used … but frankly speaking, it’s avoiding to deal with concurrent access and I created C side shared object for this which doing the job pretty well.

*         More problematic : strings comparison doesn’t work at all

If my_var == “toto”

                Will always fails. I got from this list it’s because there is a lookup table to speed up and this table is stick to the main State.

 What would be the best solution to avoid this 2nd nasty issue ? Is implementing lua_(un)lock()still the solution in 5.3 ? Does it mean I will have only a single Lua_State shared with all threads ?

Any better solution

 

Thanks

 

Laurent



It is possible to add preemptive scheduling to Lua >5.2 using hooks and yields. But I would ask myself first, if you really need preemptive scheduling and in which situations.
So my advice is, to look into a cooperative solution for your problem first.
--
Thomas