I’m trying to invoke a function in a native C library using JNA which uses GLib and GLib Threads. When I attempt to invoke the function, the JVM crashes and the following error is printed:
GLib-ERROR **: The thread system is not yet initialized.
aborting...
I’m pretty sure that means the code needs to invoke the g_thread_init function in the gthread library before invoking the target function. Thus, I defined the following interface to load the gthread library…
public interface GLibThreads extends Library {
public static final GLibThreads INSTANCE =
(GLibThreads) Native.loadLibrary("gthread-2.0", GLibThreads.class);
public void g_thread_init(Pointer pointer);
}
…and I tried invoking it before the target function…
NativeLibrary.getInstance("glib-2.0");
GLibThreads.INSTANCE.g_thread_init(Pointer.NULL);
String flavors = LibSoda.INSTANCE.getFlavors();
…but unfortunately, this doesn’t work. I still get the “thread system is not yet initialized” error.
I thought my problem might have been related to defining the method to take a JNA Pointer. Here’s how the g_thread_init function is defined in the gthread header:
void g_thread_init (GThreadFunctions *vtable);
So I tried defining the GThreadFunctions struct and passing in null. Unfortunately, that didn’t make any difference, and I still get the “thread system is not yet initialized” error.
How can I initialize the threading system so I can successfully invoke the target function?
Don’t load the glib library before calling g_thread_init.
NativeLibrary.getInstance(“glib-2.0”) explicitly loads glib. While calling g_thread_init eventually implicitly loads glib (depending on the OS’s own dependency tracking), it probably does so after setting up whatever parts of gthread that trigger the error if not set up.