lua-users home
lua-l archive

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


> 3) Can multiple Lua States coexist nicely?

Yes. See http://www.thijsschreijer.nl/blog/?p=693 

> 
> 4) In a real world use case my users have many scripts in my DSEL that
> get executed at a frequency of about 30 Hz or more. Would I create a Lua
> state and execute the chunk every time or can I re-use the state after
> instantiating it once and excecute many times?

You might have to cleanup some stuff left behind in between calls, probably using some sandboxing that can be discarded.

Generally two approaches;
1) use a single state. In this case you can use coroutines to create different environments (cooperative multithreading)
2) use multiple states. Using OS threads (preemptive multithreading)
 
1: might require protection from a script locking/hanging the environment too long to keep it fluent. Will only utilize a single CPU core. Easier to share global state between the different scripts. In a multithreaded application synchronization happens before entering the Lua state.
2: This is capable of utilizing more CPU cores, but involves more complexity when global state needs to be shared. At any given time only a single thread can enter a Lua state safely. In this case synchronization happens between the states to share global data.

And obviously many in between variants are possible. Best pick really depends on your use case, shared data or not, etc.

Caveat; some underlying c functions used by Lua are not thread safe (I think rand(), some date functions, etc.). This tends to be platform specific, but you should verify.

> 
> As you can see, I am a Lua newbie and just hope to get some answers in
> order to get a grip on the question if Lua is a suitable alternative for
> the DSEL scripts. Any hints are appreciated.
> 
> Thanks,
> Moose
>