[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Caching the result of require() across instances (multithreading / embedded).
- From: John Craws <john.craws@...>
- Date: Thu, 4 Oct 2012 15:13:39 -0400
Hi everyone,
I am trying to implement the following and could not manage to find a
solution yet.
I have a multithreaded C++ application where multiple threads embed
Lua via the API, load bytecode from a buffer and excute it, passing in
parameters and collecting the results.
The locked bytecode buffer is created at startup by loading the Lua
files and dumping the results. When a thread needs to execute the
code, it locks the buffer, retrieves the bytecode, loads it into its
private Lua instance, locks again, and runs. This works.
The problem occurs when one of the Lua files uses require(). With the
default loaders, there is no way (that I see) to share the result, so
each thread has to load the module. Consequently, the .so and .lua
files are reopened and reloaded every time. Is there a way to share
the result and have require() load once for all threads/instances (as
it does for multiple calls for the same module in the same
thread/instance)?
I'm trying to find a solution that would work yet remain relatively
simple to implement. I don't need any more multithreading/sharing
functionality than this for the moment.
I looked at multithreading modules (lanes, etc), precompiling and
linking with LuaJIT, custom loaders, patching Lua itself, having a
separate instance that would hold the bytecode, etc. I'd really
appreciate if someone with more knowledge could point me in the right
direction.
I'm using Lua-5.1.4 on Linux.
Thanks a lot,
John Craws