lua-users home
lua-l archive

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




On 16 Jun 2019, at 15:44, Andrew Gierth <andrew@tao11.riddles.org.uk> wrote:

"Abhijit" == Abhijit Nandy <abhijit.nandy@gmail.com> writes:

Abhijit> Hi,

Abhijit> I have a Lua module(DLL) which I want to use in two different
Abhijit> states which run in two different threads. There is a mutex to
Abhijit> prevent access to the module by both threads simultaneously.

Abhijit> If I load the module in the first state, then can I load it in
Abhijit> the second thread and use it(given that there is a mutex
Abhijit> preventing use from both threads and therefore states,
Abhijit> simultaneously). Could there be any issues if a module is
Abhijit> loaded in two different states in the same application?

This should work in principle (assuming the module itself doesn't do
anything wrong).

Each lua state maintains its own independent table of loaded modules, so
dlopen/LoadLibrary will be called twice; for both functions, this is
specifically allowed, and a reference count of the number of opens is
kept by the system so that the module isn't unmapped until it has been
closed as many times as it was loaded.

--
Andrew.


Andrew is right, but there is a caveat; if the loaded module does not store its state in a the Lua state (but in its own global data somewhere), then it might actually share that data across multiple Lua states and hence cause problems.

Old post, but still valid: http://www.thijsschreijer.nl/blog/?p=693

So check the dll code to make sure it only stores state in the Lua state, and you should be good.

Thijs