lua-users home
lua-l archive

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


Le Samedi 6 juin 2015 22h22, Nagaev Boris <bnagaev@gmail.com> a écrit :



Thanks Boris for your reply. Searching for additional information on ideas you provided, I found this interesting page : http://lua-users.org/wiki/ThreadsTutorial

First of all, but if I rewrite entirely the MQTT stack, I have to keep my current implementation where Paho library spawn a new C thread to handle incoming messages. The question now is how to get the new Lua_stat ?

- I rejected the use of lua_open() as I need access to global variables and I don't want to reopen each and every libraries.

- so let's go with lua_newthread() : as per my understanding (let me know if I missed anything), it will not spawn really a new thread (in system point of view) but "only" create a kind of forked Lua stat allowing my thread to live its local life but still being able to interact with global environment.

But, as per this wiki, I'll have to create my own locking function, right ?

- I noticed also lua_newstate() but I wasn't able to determine if global variables are still accessible or not.


So, all in all, my new code may looks like something like :

struct _context {
    lua_State *L;
    ... other context stuffs, bla bla ...
};

int msgarrived(void *actx, char *topic, int tlen, MQTTClient_message *msg){
    struct _context *ctx = (struct _context *)actx;    // to avoid zillion of casting

    ...

    lua_pushnil(ctx->L);    // push nil in the alternate Lua stack

    ...
}

void myinit( lua_State *L){
    struct _context ctx;     // in fact, it will not be a stack object, but it's only an example

    ctx.L = lua_newthread( L);

    MQTTClient_setCallbacks( client, &ctx, connlost, msgarrived, NULL);
}

Best regards,

Laurent