1

I would like to load a Lua script into my C++ program, and then invoke that script multiple times in separate threads. I'm trying to avoid loading the script in each thread (why go through that overhead).

I'm thinking something like this in the C++ program:

create lua state L
load script into L

and in n threads do:

create local lua state Si   (i = 1..n, i.e., separate state per thread)
grab "compiled" script from L and invoke in the context in Si

Is there a "standard" approach to doing this? The primary goal is to avoid having each thread load the script. The script may also be executed multiple times in state Si. Note that the scripts running in separate threads are not cooperating (i.e., they know nothing about each other and we would like to keep it that way).

2
  • 1
    Are you talking about C++ threads or lua threads? Lua also has a concept of threads that dont actually mean different program threads. Also dont think you can do what you want without data races or mutexes everywhere basically forcing single state. You must run the script on each state and each state must be a separate state. Commented Jul 15, 2016 at 21:10
  • I'm talking about separate c++ threads. Main program loads the script in state L. Later C++ threads are created that want to apply script created in L, but since they are new C++ threads and there can be multiples of these threads running the same script at the same time, I think you want the script to be executed in a state specific to the thread. My pidgin code above is C++ code. Commented Jul 15, 2016 at 21:19

1 Answer 1

1

As I said in a comment, I dont think you can do what you want with c++ threads without data races or mutexes that block all parallel execution. A single lua state does not seem to be designed to be used by multiple threads and lua threads dont support multithreading either and there is no way to move data from a completely separate state to another magically.

However what you can try is to "compile" the lua scripts on a state by doing loadstring and then dump and save that in a safe way so that you can access it from all threads to load the script faster from the bytecode directly with loadstring.

Otherwise you would need to keep all states separate and do all communication between them by C++ and ensure thread safety there.

Sign up to request clarification or add additional context in comments.

2 Comments

I think the answer seems to be that lua objects can't be shared between states. It is unfortunate that lua doesn't have a concept of "nested" states, i.e., I parse a script in the global state, I create a nested state that can access the global state (but keep changes in the local state).
@mkilian lua has nested states, but you cannot use them from multiple c++ threads at the same time. Really the problem is that you cannot do anything in a thread safe manner. Every access to lua must be guaranteed to be single threaded due to it's design.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.