-
-
Notifications
You must be signed in to change notification settings - Fork 591
Closed
Labels
Description
We observed a problem when calling a utility function like Loader.isLoadLibraries
(which calls Loader.load()
when this is the first access) in one thread while running e.g. Pointer.maxBytes()
in another thread:
// thread 2
public class PrintMaxBytes implements Runnable {
@Override
public void run() {
long l = org.bytedeco.javacpp.Pointer.maxBytes();
System.out.println(l);
}
}
var t = new Thread(new PrintMaxBytes ());
t.start();
// thread 1(calls Loader.load())
Loader.isLoadLibraries();
- let thread 2 start class loading / static init block in
Pointer
up until, BUT excludingPointer
line 521. - then let thread 1 run to execute the real
Loader.load();
triggered byLoader.isLoadLibraries()
- -> deadlock!
The reason seems to be that in the JNI code there is
jclass cls = (jclass)env->CallStaticObjectMethodA(JavaCPP_getClass(env, 0), putMemberOffsetMID, args);
which basically calls the static method Loader.putMemberOffset
and by doing so triggers another call to Loader.load()
for which the other thread already has a lock.
In our case we solved the issued by doing e.g. new Pointer(0)
in an early part of the code where we are sure we still have only one thread.