是否可以在GLIB循环中运行GLIB环?

发布于 2025-01-21 18:44:13 字数 1195 浏览 2 评论 0原文

只是想知道这是否可能。我一直从内部循环的一个周期/运行中得到响应。这是我的代码

struct app{
  GMainContext *context;
  GMainLoop *loop;
}app1;

int counter = 0;

static void
condition_responses()
{
  g_print(" counter is %d \n", counter);
  counter++;

}

static void
check_cmd()
{ 
   //// other commands

   if(some-condition)
   {
       GSource* source1 = g_timeout_source_new(1000);

       // set function to watch for command to quit
       g_source_set_callback(source,
                          (GSourceFunc)condition_responses,
                          NULL,
                          NULL);

       g_source_attach(source1, app1.context);

   }
}

int main()
{
    GSource* source = g_timeout_source_new(1000);

    // set function to watch for command to quit
    g_source_set_callback(source,
                          (GSourceFunc)check_cmd,
                          NULL,
                          NULL);

    g_source_attach(source, app1.context);

    g_main_loop_run(app1.loop);

    g_print("task ended \n");

}

,以某种方式运行了内部循环conditonal_respons,但它仅运行一次。我需要启动一些功能并定期运行它们。我有一个问题是

  • 创建glib循环的正确方法吗?
  • 我还可以做什么选择?
  • 如果我创建一个g_thread去做会发生什么?

谢谢

Just wondering if this is possible. I have been getting response from one cycle/run of the inner loop. This is my code

struct app{
  GMainContext *context;
  GMainLoop *loop;
}app1;

int counter = 0;

static void
condition_responses()
{
  g_print(" counter is %d \n", counter);
  counter++;

}

static void
check_cmd()
{ 
   //// other commands

   if(some-condition)
   {
       GSource* source1 = g_timeout_source_new(1000);

       // set function to watch for command to quit
       g_source_set_callback(source,
                          (GSourceFunc)condition_responses,
                          NULL,
                          NULL);

       g_source_attach(source1, app1.context);

   }
}

int main()
{
    GSource* source = g_timeout_source_new(1000);

    // set function to watch for command to quit
    g_source_set_callback(source,
                          (GSourceFunc)check_cmd,
                          NULL,
                          NULL);

    g_source_attach(source, app1.context);

    g_main_loop_run(app1.loop);

    g_print("task ended \n");

}

Somehow the inner loop conditonal_responses run but it ran only for once.I would need to start some function and to run them periodically. Some question that I have

  • Is this the right way to create the loop in glib?
  • What other options could i go about doing them?
  • What would happen if i create a g_thread to go about doing it?

Thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文