lua-users home
lua-l archive

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


At 13:21 29/10/2007, you wrote:
Hello yall,

I'm developing a multithread system, a server answering to a lot of connections at the same time. I'm using Lua to easier the job of having custom made versions. Everything is in C++ and I'm using Luna.h.

Now I'm at the point of choosing how I will use multi thread with Lua. For now I implemented a lua pool (no queues yet) that gives me an already initialized lua_State* whenever I need one. So, I open a new thread (using PThreads on Linux), get an unused lua_State* from the pool, send the inputs, run the script and get the results back. But this model has 2 problems to me: 1 - Scope pollution: and second time I run the script with the same lua_State* from the pool, the global variables set by the first run are still there. Is there a fast way to clean only the variables set by the Lua script? 2 - Global variables are not global anymore. One script doesn't have access to the global variables set by another instances. On thread doesn't talk to the other using lua entities: numbers, tables, etc...

I only need a solution to one of the items, the second preferentially.

But, maybe this model is not good at all. What are you using to solve the multi thread problem?

Do you have control over the scripts being used?

If so, I wouldn't use Lua globals at all if you want them to be 'application global' rather than 'script global'. Instead have some global access functions in C (available to your Lua scripts) to set/retrieve 'globals' from your C++ program directly. These can then do the multi-access protection to make sure that only one thread is accessing them at once, etc

So, instead of

x = myglobal;

have

x = getglobal("myglobal");

This is what we do here for some things, but we don't use Luna, so I can't give example Luna code for the global access/retrieve functions



Paul                            VPOP3 - Internet Email Server/Gateway
support@pscs.co.uk                      http://www.pscs.co.uk/