lua-users home
lua-l archive

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

As far as I understand it, the only ways to use Lua in a multithreaded environment are:
1. define lua_lock() and lua_unlock() macros as mutexes (but this pretty much makes it single-threaded in effect)
2. use entirely independent lua_States in different threads, and ferry messages between them
messages can be serialized (e.g. Lanes) or converted to an intermediary format (e.g. ORB etc)

1) can be rejected in most cases, since the most of the benefits of multi-threading are lost

Having independent lua_States is the only alternative, since related states share their registry and more importantly share the garbage collector, which is clearly not thread-safe!

Perhaps there may be a way to produce a more efficient, lower-level serialization using the Lua internals (which would appear as a conversion to 'immutable' status), I'm not sure. There are many nontrivial issues however:  function upvalues, tables with cycles, userdata, etc. 

Regarding the fifo: I also think that a notion of Futures would be a useful addition to Lua:
Something similar can be found in Helper Threads - did you look at that?  

I've experimented with something similar for sharing C++ structures between threads; one thread is the owner and can modify the structure, other threads can only read.  Still, only fixed-size structures (such as arrays) are safe in this context (safe but not necessarily usable...), some structures can be made usable with double-buffering, and care needs to be taken over the life-cycle of objects (e.g. proxies, reference counts or more advanced techniques). 


On Apr 12, 2008, at 11:37 AM, Mildred wrote:

I'm thinking about how it could be possible to get Lua working in a
preemptive multithreaded environment. And I was thinking about a way
this could be done without sharing writable memory.
Well, the idea isn't new, it comes from the COP model that can be found
on the Lisaac language documentation <>
(but not yet implemented).

So the idea is to be able to have multiple threads running
concurrently. Those threads won't be able to have shared writable
objects. If they share an object, this object must be read-only. Thus
it solves the problem when multiple threads are modifying the same
object at the same moment.

The idea continues to allow threads to call functions on other threads.
These function call are stored in a fifo queue and executed when
possible. If a function call requires a result, the call is blocking.
Otherwise it's not blocking. And of course, all arguments to these
function call are transformed into immutable objects (read-only) before
the call. This should be recursive (table members, environment,
metatables ...)

So I was wondering how it could be done in Lua. Actually, I don't have
any knowledge about Lua internals, that's why I'm asking you.
I think that the only thing that is needed to implement this
cooperative model is the ability to:
- make an object immutable
- be able to copy (or perhaps make a reference) immutable objects from
one lua universe to another, without creating side effects that would
cause problems when the two lua universes are used concurrently (that
should be possible if the objects are immutable)

Do you think it's possible ? Easy to do ?

I know that it is possible to serialize objects ... But somehow I don't
think that's the right solution. And I don't think you can serialize
everything (how do you serialize functions and their upvalues ?)


Mildred Ki'lya
E-Mail: mildred593(at)

XMPP: <> (GoogleTalk, Jabber)

GPG: 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]

Be seeing you

grrr waaa