GThread中的g_timeout_add

时间:2012-07-01 23:03:16

标签: multithreading timeout glib

我正在尝试添加特定于特定GThread的超时源。

在主线程中,我可以创建一个GMainContext( g_main_context_new )并添加超时( g_timeout_add )。但是,当我尝试在用 g_thread_create 创建的线程中执行它时,它根本不起作用,GSourceFunc永远不会被调用,我不知道为什么。

目前我只收到了这份文件:

  

回调需要一点关注。来自GTK +的回调(信号)   是在GTK +锁内制作的。然而来自GLib的回调(超时,   IO回调和空闲功能)是在GTK +锁之外进行的。   因此,在信号处理程序中,您不需要调用   gdk_threads_enter(),但在其他类型的回调中,你可以。

但我的超时功能(用于测试目的)仅在控制台中打印,因此我认为这不是资源保护和互斥锁的问题。

线程结构是:

  

主线程 - >没有显式创建GLib主要上下文

     
      
  • 捕获线程

  •   
  • 流程主题 - >应该有一个GLib主要上下文和一个超时源

  •   
  • show thread

  •   

我感谢任何帮助。

提前致谢。

1 个答案:

答案 0 :(得分:1)

您使用g_timeout_add()还是g_source_attach()

g_timeout_add()g_timeout_add_full()不允许您指定要添加的主要上下文。它始终使用默认主要上下文。如果您没有在主线程中使用默认主要上下文,则可以在process thread中使用它。您可以在description

中阅读相关内容
  

GMainContext只能在单个线程中运行

默认主要上下文由许多函数隐式创建,包括g_main_context_default()。因此,请确保您没有在主线程中使用它。

如果您决定这样做,可以使用g_source_attach()将超时源添加到您自己的主要上下文中。没有可用于指定主要上下文的超时功能。所以,就这样做吧。

以下代码基本上与:g_timeout_add_full(G_PRIORITY_DEFAULT, 100, func, l, notify);

相同
#include <glib.h>

void notify(gpointer data)
{
        g_main_loop_quit((GMainLoop *)data);
}

gboolean func(gpointer data)
{
        static gint i = 0;
        g_message("%d", i++);
        return (i < 10) ? TRUE : FALSE;
}

gpointer thread(gpointer data)
{
        GMainContext *c;
        GMainContext *d;
        GMainLoop *l;
        GSource *s;

        c = g_main_context_new();
        d = g_main_context_default();

        g_message("local: %p", c);
        g_message("default: %p", d);

#if 1
        l = g_main_loop_new(c, FALSE);
        s = g_timeout_source_new(100);
        g_source_set_callback(s, func, l, notify);
        g_source_attach(s, c);
        g_source_unref(s);
#else
        l = g_main_loop_new(d, FALSE);
        g_timeout_add_full(G_PRIORITY_DEFAULT, 100, func, l, notify);
#endif

        g_main_loop_run(l);
        g_message("done");

        return NULL;
}

int main(int argc, char *argv[])
{
        GError *error = NULL;
        GThread *t;

        g_thread_init(NULL);
        t = g_thread_create(thread, NULL, TRUE, &error);
        g_thread_join(t);

        return 0;
}
相关问题