tags:

views:

163

answers:

3

Hello, 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

+2  A: 

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?

djna
+1  A: 

This doesn't seem possible. The System.loadLibrary method doesn't have any arguments or documentation that would indicate you could load the library in a thread-specific manner or to load the same library twice.

If you had a specific number of threads, you could make multiple libraries with slightly different naming schemes. Otherwise, you should probably just implement the locks.

Dave
+1  A: 

It is not possible. This would be equivalent to using POSIX dlopen etc to dynamically load multiple copies of a C / C++ library into C / C++ program, and that doesn't work either.

Your primary options are:

  • modify the C library to make it thread-safe

  • recode it in (thread-safe) Java

  • wrap it as an application and run multiple copies as separate processes

  • wrap it as a service and run multiple copies, talking to it using Sockets or an appropriate higher-level RPC mechanism.

Stephen C