如何使用 JNA 调用 GLib 线程 g_thread_init?

发布于 2024-11-16 20:16:34 字数 1074 浏览 6 评论 0原文

我正在尝试使用 JNA 调用本机 C 库中的函数,该 JNA 使用 GLib 和 GLib 线程。当我尝试调用该函数时,JVM 崩溃并打印以下错误:

GLib-ERROR **: The thread system is not yet initialized. 
aborting...

我很确定这意味着代码需要调用 gthread 中的 g_thread_init 函数在调用目标函数之前先调用库。因此,我定义了以下接口来加载 gthread 库......

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);

}

并且我尝试在目标函数之前调用它......

NativeLibrary.getInstance("glib-2.0");

GLibThreads.INSTANCE.g_thread_init(Pointer.NULL);
String flavors = LibSoda.INSTANCE.getFlavors();

但不幸的是,这不起作用。我仍然收到“线程系统尚未初始化”错误。

我认为我的问题可能与定义采用 JNA Pointer 的方法有关。以下是 g_thread_init 函数在 gthread 标头中的定义方式:

void    g_thread_init   (GThreadFunctions       *vtable);

因此,我尝试定义 GThreadFunctions 结构并传入 null 。不幸的是,这没有任何区别,我仍然收到“线程系统尚未初始化”错误。

如何初始化线程系统以便成功调用目标函数?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你与昨日 2024-11-23 20:16:34

在调用 g_thread_init 之前不要加载 glib 库。

NativeLibrary.getInstance("glib-2.0") 显式加载 glib。虽然调用 g_thread_init 最终会隐式加载 glib(取决于操作系统自身的依赖项跟踪),但如果未设置,它可能会在设置触发错误的 gthread 的任何部分之后这样做。

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.

始终不够 2024-11-23 20:16:34

在调用 g_thread_init 之前不要加载 glib 库。

当我删除 NativeLibrary.getInstance("glib-2.0"); 代码行时,线程系统初始化并且对 LibSoda.INSTANCE.getFlavors() 的调用成功!

我不完全确定为什么这有效。我知道访问 gthread-2.0 库会隐式加载 GLib,但我不知道为什么首先显式加载 GLib 会导致 g_thread_init 调用被忽略。

我很乐意将接受的答案更改为解释上述工作原理的答案。

Don't load the glib library before calling g_thread_init.

When I removed the NativeLibrary.getInstance("glib-2.0"); line of code, the thread system initialized and the call to the LibSoda.INSTANCE.getFlavors() succeeded!

I'm not entirely sure why this worked. I know accessing the gthread-2.0 library will implicitly load GLib, but I don't know why loading GLib explicitly first causes the g_thread_init call to be ignored.

I'll gladly change which answer is accepted to an answer which explains why the above worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文