I found a way that is working for months now (after a long fight and some help form this mailing list :) ) : you can have a look on my application named Séléné (https://github.com/destroyedlolo/Selene/) which is a scripting automation tool driven by MQTT messages arrival.
It is used to drive my smart home automation as well to build a dashboard using DirectFB plug-in.
All user scripts are made in Lua whereas all the backend, technical layers, MQTT, DirectDB handling is made in C.
The main thread has it's own Lua_State which load the code and stores global stuffs (functions, ...).
Due to MQTT library design, each MQTT messages arrival is handled by a separated thread : in this new thread, I'm creating a new dedicated lua_State, and retrieving some information (like the function to call) from the main thread's state, protected by a mutex (the only moment I'm blocking the main thread).
All in all, I got several totally separated threads running in parallel.
They are exchanging data using shared object which are protected by dedicated mutex (so only 1 object is blocked during access), managed at C side.
Perhaps my explanation is not very clear, but I hope the corresponding code is quite understandable.
All in all, I'm very satisfied by this solution which is :
- safe
- efficient
- very easy to work with.
Bye
Laurent