Où sommes nous ?

Retour

multi-threading discuss

As of now, Séléné is able to launch an additional thread in 2 circumstances with consequences ont the environment the called function will got.

MQTT message arrival

A function may be launched when a message arrives for a subscribed topic, thanks to func= argument.

_, err = Brk:subscribe( {      
{ topic = "toto",func=handle_toto }
} )
if err then
print( err )
return
end

In this case, handle_toto will be launched in a separate thread for each message received for toto topic.

Nothing prevent the callback function to declare "global" objects ... but they will be available only to ... itself. Again, its context is not shared with anything else.

Such callback function doesn't inherit the global context and so, can't access to any global object. SelShared is the way to exchange data between threads as well as launching tasks in the main thread (have a look on dedicated pages).

It's a common but very frustrating error to forget object's scope. If you encounter an error about undefined object ("... nil value ..." or something), make sure you're not inside a callback function. A good tip is to display the content of _G to check which objects are available ... and very few of them are available for callbacks.

Detach

Selene.Detach() is launching a function in a totally separated thread, with a dedicated context : Unlike Lua's co-routines, both the main thread and the new one are running independently, in a full parallel and secure way.

As of Séléné v3.18, the detached function can get access to global variables, as demonstrated in Detach example.

If it is safe to parallel read only access to a variable, there is no mechanism in Lua to arbitrate concurrent access, leading to potential data loss, corruption or even crashes. If you need write access to an object shared among thread, it is strongly advised to use SelShared.


Visitez :
La liste de nos voyages
Nos sorties Ski et rando
Copyright Laurent Faillie 2001-2024
N'oubliez pas d'entrer le mot de passe pour voir aussi les photos perso.
Contactez moi si vous souhaitez réutiliser ces photos et pour les obtenir avec une plus grande résolution.
Visites durant les 7 derniers jours Nombre de visites au total.

Vous pouvez laissez un commentaire sur cette page.