[Lua] Multi threading Lua...

Started by
24 comments, last by ZealGamedev 13 years, 6 months ago
Quote:Tables are garbage collected, they aren't allocated on the stack


Really? I always assumed all local allocations avoided the GC and just got deallocated when they went out of scope. Maybe some special cases are needed for closures/upvalues? But thats not the case in my example...

So it sounds like the GC is the primary reason why there is a problem (although you mentioned there could be other problems, like hooks ect...)?
Advertisement
Quote:Original post by ZealGamedev
Quote:Tables are garbage collected, they aren't allocated on the stack


Really? I always assumed all local allocations avoided the GC and just got deallocated when they went out of scope. Maybe some special cases are needed for closures/upvalues? But thats not the case in my example...

So it sounds like the GC is the primary reason why there is a problem (although you mentioned there could be other problems, like hooks ect...)?


I think the moral of the story here is that without intimate knowledge of Lua's inner workings you should do what the developer recommends. Ask yourself if it's really worth your time to fight it, or should you instead accept what it is and work within those constraints. Maybe you've determined that it is worth your time, I don't know...
Quote:
I think the moral of the story here is that without intimate knowledge of Lua's inner workings you should do what the developer recommends. Ask yourself if it's really worth your time to fight it, or should you instead accept what it is and work within those constraints. Maybe you've determined that it is worth your time, I don't know...


Oh I agree. While I am 100% confident this problem could be solved (for starters, a thread safe GC like Sneftel suggested), I am also 100% confident it would require a lot of work. Like I said, I am just surprised this issue has not been addressed before (maybe it has?).

I suppose moving all my data into c and routing all communication through there is looking more and more like the best option. Perhaps the benefits (as described by the bitsquid guys) will out weigh the costs.. Still a shame though.. if only the lua GC/threads behaved slightly differently...
Quote:Original post by ZealGamedev
Like I said, I am just surprised this issue has not been addressed before (maybe it has?).

It was a design trade-off. There is typically no easy answer to such questions.

Making each lua_State thread safe would be very expensive in a single-threaded system. Additionally, the benefits might not be great because such locking inhibits parallelism, and such locks are likely to be highly contended.

Threading is platform dependant, Lua is AFAIK written in pure C with little or no platform specific code.
Quote:Original post by ZealGamedev
I suppose moving all my data into c and routing all communication through there is looking more and more like the best option. Perhaps the benefits (as described by the bitsquid guys) will out weigh the costs.. Still a shame though.. if only the lua GC/threads behaved slightly differently...


well not all your data, maybe just data that needs to be thread safe. Personally I solved the problem by exposing my event framework (which is inherently thread safe) to Lua so I can trigger events from Lua and listen for/handle events in Lua. If I need to send some data to another thread, I package it in an event and set up a listener in the other thread. I find that working through a framework is a solid way to implement threading rather than worrying about and solving the same problems in many places.
Quote:Making each lua_State thread safe would be very expensive in a single-threaded system. Additionally, the benefits might not be great because such locking inhibits parallelism, and such locks are likely to be highly contended.


It wouldnt necessarily have to be all that expensive. I am sure there are some clever lock free designs that would work. And it could always be a optional feature you choose when building (kinda like the current optional lua_lock/unlock defines). However, for now I think we are going to just try the two state solution and migrate all our data into C. Perhaps in the future we will revisit this topic (assuming the two state solution doesnt work out).

Anyway, thanks everyone for the input. I had just assumed that a lua_newthread() state would be independent of the main state/GC at LEAST for local allocations, but now I know.

This topic is closed to new replies.

Advertisement