Search code examples
javadll

Java: Load the same dynamic library in two threads (both threads in the same JVM)


I am using a library (written in C) that is not reentrant(i.e no function in the library is reentrant). Suppose I have loaded the library via System.load to get the handle say 'v'. I cannot use v in two threads because of the reentrancy issues (tried but nonsense results). I could use locks, but that defeats any parallelism i could have gained.

What I'd like to do is start two threads, and in each thread load the library to get two different handles(thus there are two copies of the loaded library).

Is this possible in Java? Regards Saptarshi


Solution

  • Any DLL can be loaded only once by a process, so I don't think you can achieve what you want. You could cheat and rename the DLL to a different name maybe?

    Your threads spend so much time in the DLL that there is no other parallelism to be had?