gpt4 book ai didi

glib - 如何将 glib 信号回调附加到特定线程上下文

转载 作者:行者123 更新时间:2023-12-02 03:36:38 27 4
gpt4 key购买 nike

所以基本上我正在编写以下步骤来创建基于 dbus 的应用程序。1. g_bus_watch_name(特定服务出现的回调)2. g_signal_connect 附加到服务提供的某些信号。 g_signal_connect(代理,"xyz", G_CALLBACK(回调), NULL);

我希望“回调”在特定线程上下文下运行。

任何提示将不胜感激

最佳答案

来自the GDBusProxy documentation :

A GDBusProxy instance can be used from multiple threads but note that all signals (e.g. “g-signal”, “g-properties-changed” and “notify”) are emitted in the thread-default main context of the thread where the instance was constructed.

因此请确保 GMainContext您希望在创建 GDBusProxy 时以线程默认方式发出信号。典型的模式是调用 g_main_context_push_thread_default()在线程函数的开始处,然后创建 GDBusProxy 并将信号连接到它,并将对象完全保留在该线程内。

有关使用 GMainContext 的推荐模式的更多信息,请参阅 the tutorial 。特别是,它建议每个线程(该线程的默认值)有一个 GMainContext,并且不要在线程之间移动它们。虽然受支持,但在线程之间移动上下文的速度很慢(由于锁定),并且使得其余代码的控制流和线程安全性很难推理。

关于glib - 如何将 glib 信号回调附加到特定线程上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50020800/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com