lua-users home
lua-l archive

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


Hmm. I'm not sure if this is going to work for me. My main (system) thread is spending almost all of its time jumping in and out of Lua, occasionally wanting to send data to the secondary (system) thread. The secondary thread needs to read this input data and behave accordingly, and ideally this would all happen in Lua, or at least as much as possible. Not only do I not need my secondary lua_State to have global references to the first, I'd actually have to ensure that it is not possible, since both will be concurrently active.

Using lua_lock / lua_unlock seems an unnecessary burden for my situation, since communication is essentially one-way (and efficiency is very important).

I guess I'll need to write my own message send / receive methods in C to implement a simple message queue between lua states.

Q: if a thread created using lua_newthread modifies 'global' data, is this modifying the global data in the original thread, or modifying copied data in the new thread?

On Sep 16, 2006, at 2:07 AM, lists@grahamwakefield.net wrote:

On Sep 15, 2006, at 5:15 PM, Luiz Henrique de Figueiredo wrote:

How much can I copy Lua data around between distinct
states (assuming thread safety is managed correctly)?

If you have got your locking right, you can use lua_xmove if the Lua
states are related (ie, children of the same Lua top state).

Aha! I didn't realise what lua_newthread did (I thought it was something to do with coroutines). OK. So if I understand correctly, I can have a root lua_State in one thread, create new lua_States from it using lua_newthread(), which will be managed in other threads, but create thread-safe interchanges between them using lua_xmove.

So lua_xmove is really creating copies from one stack to another. How are references resolved - are they deep copies?

lua_newthread retains references to global data; how can this be thread safe if both the old & new threads (running in different system threads) can access them? Are they also independent copies?

Sorry if I'm not understanding something here.

According to the manual:

lua_newthread

lua_State *lua_newthread (lua_State *L);
Creates a new thread, pushes it on the stack, and returns a pointer to a lua_State that represents this new thread. The new state returned by this function shares with the original state all global objects (such as tables), but has an independent execution stack.

There is no explicit function to close or to destroy a thread. Threads are subject to garbage collection, like any Lua object.