lua-users home
lua-l archive

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


If you mean the lua_State* that represents your running program, no it is not thread safe by default. There are probably a few kinds of things that are safe to do in combinations from two or more threads, but that is probably an implementation detail rather than a language feature.

For instance, I could imagine you safely reading a global from two threads if it doesn't do Bad Things™ to the stack (it might..?), but there's nothing to stop you from reading and writing the same global variable from two threads at the same time, which would be bad.

On Sat, Jun 6, 2015 at 9:50 AM, Laurent FAILLIE <l_faillie@yahoo.com> wrote:
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 misspelling master is on the Web.
  _________ 100 % Dictionnary Free !
/               /(
/ Dico       / / Pleins d'autres fautes sur
/________/ /
(#######( / http://destroyedlolo.info
Quoi, des fautes d'orthographe! Pas possible ;-D.



Le Mercredi 3 juin 2015 15h03, Laurent FAILLIE <l_faillie@yahoo.com> a écrit :


Ok, thanks to all for your replies.

I'll make some tests with the provided information and will post the result (if it can help someone else).

Thanks

Laurent----
The misspelling master is on the Web.
  _________ 100 % Dictionnary Free !
/               /(
/ Dico       / / Pleins d'autres fautes sur
/________/ /
(#######( / http://destroyedlolo.info
Quoi, des fautes d'orthographe! Pas possible ;-D.



Le Mercredi 3 juin 2015 14h19, Roberto Ierusalimschy <roberto@inf.puc-rio.br> a écrit :


>  Another way is to store a reference in the Lua Registry:

>      int myfuncref;
>     
>      static int foo(lua_State *L)
>      {
>       
>        luaL_checktype(L,1,LUA_TFUNCTION);
>        myfuncref = luaL_ref(L,LUA_REGISTRYINDEX);
>        lua_pushinteger(L,myfuncref);
>        lua_pushvalue(L,1);
>        lua_settable(L,LUA_REGISTRYINDEX);
>        return 0;
>      }

luaL_ref does all the work (see the manual). This function should be
like this:

      static int foo(lua_State *L) {
          luaL_checktype(L,1,LUA_TFUNCTION);
          myfuncref = luaL_ref(L,LUA_REGISTRYINDEX);
          return 0;
        }


>  And to call that:

>      static int bar(lua_State *L)
>      {
>        lua_pushinteger(L,myfuncref);
>        lua_gettable(L,LUA_REGISTRYINDEX);
>        lua_call(L,0,0);
>        return 0;
>      }

(It is simpler to use 'lua_rawgeti'...)

-- Roberto








--
Brigham Toskin