c++ - Load Lua script once, execute in multiple states in multiple threads -
i load lua script c++ program, , invoke script multiple times in separate threads. i'm trying avoid loading script in each thread (why go through overhead).
i'm thinking in c++ program:
create lua state l load script l
and in n threads do:
create local lua state si (i = 1..n, i.e., separate state per thread) grab "compiled" script l , invoke in context in si
is there "standard" approach doing this? primary goal avoid having each thread load script. script may executed multiple times in state si. note scripts running in separate threads not cooperating (i.e., know nothing each other , keep way).
as said in comment, dont think can want c++ threads without data races or mutexes block parallel execution. single lua state not seem designed used multiple threads , lua threads dont support multithreading either , there no way move data separate state magically.
however can try "compile" lua scripts on state doing loadstring , dump , save in safe way can access threads load script faster bytecode directly loadstring.
otherwise need keep states separate , communication between them c++ , ensure thread safety there.
Comments
Post a Comment