|
On Fri, Apr 4, 2014 at 7:59 AM, Javier Guerra Giraldez <javier@guerrag.com> wrote:
On Fri, Apr 4, 2014 at 9:49 AM, Milind Gupta <milind.gupta@gmail.com> wrote:
> I think that it would be more memory efficient if I create just 1 lua state
> and run all the threads in that, each with its own environment. Is that
> true?
no
the issue is not about the environment, but about sharing the whole
state. it's a complex memory data structure fairly optimized for
single-threaded access. it's definitely _not_ threadsafe as is.
it's not too hard to wrap every access to the state with mutex locks,
but then i amount to a GIL (global interpreter lock), which makes all
Lua code serialized and with a heavy lock/unlock overhead.
long time ago, I dabbled on lock-free datastructures and managed to
create a few that could be used from Lua, but they're much harder to
get right than 'traditional' lock-based threadsafe programming. In
the end, it's better to have several Lua states that communicate by
message passing.
just my 2¢
--
Javier
You'd think that read-only access to the a shared global_State from different lua_State's (coroutines.create()) would be possible without a global interpreter lock.