c/c++语言开发共享运行多个并发GMainLoops

是否允许GLib用户在多个线程中同时运行多个GMainLoop实例,每个线程都运行自己的实例? 我发现整个地方都有“是”和“否”的答案。 我意识到之前在这个论坛(2011年12月)已经提出了这个问题。

但是,我能够在没有明显问题的情况下同时运行两个GMainLoop实例。 我的测试代码非常简单:

这样做,我看到GMainLoop两个实例GMainLoop运行得很好。 正确调用超时回调,稍后对g_main_loop_quit的调用按预期工作。

因此,让多个GMainLoop实例同时工作似乎不是问题。 但也许我只是没有充分运用API来完全掌握这种情况。 这个问题有明确的答案吗?

另外,如果有人关心的话,这是实际的测试代码:

 #define THREAD_TIMEOUTS (20) #define MAIN_TIMEOUS (1) typedef struct timeout_struct { int i; int max; GMainLoop *loop; char *name; } TIMEOUT_STRUCT; gboolean timeout_callback(gpointer data) { TIMEOUT_STRUCT *psTimeout = (TIMEOUT_STRUCT *)data; psTimeout->i++; if (psTimeout->i == psTimeout->max) { if (psTimeout->max == THREAD_TIMEOUTS) { g_main_loop_quit( (GMainLoop*)psTimeout->loop ); } return FALSE; } return TRUE; } void* thread_function(void *data) { GMainContext *ps_context; GMainLoop *ps_loop; GSource *ps_timer; TIMEOUT_STRUCT sTimeout; ps_context = g_main_context_new(); g_main_context_push_thread_default(ps_context); ps_loop = g_main_loop_new(ps_context, FALSE); sTimeout.i = 0; sTimeout.max = THREAD_TIMEOUTS; sTimeout.loop = ps_loop; sTimeout.name = "thread"; ps_timer = g_timeout_source_new_seconds(1); g_source_set_callback(ps_timer, timeout_callback, &sTimeout, NULL); g_source_attach(ps_timer, ps_context); g_main_loop_run(ps_loop); g_main_loop_quit( (GMainLoop*)data ); return NULL; } /* * This main boots a thread, then starts up a GMainLoop. Then the thread runs * a GMainLoop. The thread sets a timer that fires ten times and the main sets a * timer that fires two times. The thread quits and * and then the other main l * * * */ int main() { GThread *ps_thread; GMainLoop *loop; TIMEOUT_STRUCT sTimeout; loop = g_main_loop_new ( NULL , FALSE ); sTimeout.i = 0; sTimeout.max = MAIN_TIMEOUS; sTimeout.loop = loop; sTimeout.name = "main"; // add source to default context g_timeout_add (1 , timeout_callback, &sTimeout); ps_thread = g_thread_new("thread", thread_function, loop); g_main_loop_run (loop); g_main_loop_unref(loop); } 

    “GTK +发展基础”一书指出:

    GLib主循环实现为许多结构,允许多个实例同时运行。

    因此,鉴于此,我的测试代码以及​​我在上面评论中发布的链接 ,我们对此问题有了明确的答案。

    即:多个线程可以拥有自己的GMainContext和GMainLoop,并且能够以并发方式独立运行这些循环。

    需要了解更多c/c++开发分享运行多个并发GMainLoops,也可以关注C/ C++技术分享栏目—计算机技术网(www.ctvol.com)!

      以上就是c/c++开发分享运行多个并发GMainLoops相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/980138.html

      (0)
      上一篇 2021年12月13日
      下一篇 2021年12月13日

      精彩推荐