什么时候发出GTK信号 [英] When are GTK signals emitted

查看:102
本文介绍了什么时候发出GTK信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出从用户连接到回调函数的信号,gtk_main线程将一直休眠直到发出该信号为止.我搜索了有关发射方式或发射时间的详细信息,但找不到我不知道的任何信息.

Given a signal connected from the user to a callback function, the gtk_main thread sleeps until the signal is emitted. I searched for details on how or when it is emitted, but couldn't find any info that I don't know.

更具体地说,它是异步发出的,以便我可以在某个函数中间调用信号,或者它等待特定函数先返回吗?从GThread内部发出切换页面"信号(例如,使用gtk_notebook_remove_page())可能会产生奇怪的效果,因为事件发生在主线程中,我不能保证在主上下文中执行gtk_notebook_remove_page()就像g_main_context_invoke()用来?但是,如果我在线程内部使用g_signal_emit()手动发出信号(如果该信号可以那样发出)怎么办?

More specifically is it emitted asynchronously so that I can invoke the signal in the middle of some function or it waits for the particular function to return first? Emitting the "switch-page" signal (for example with gtk_notebook_remove_page()) from within GThread may have odd effect as the events happen in the main thread and I cannot guarantee that gtk_notebook_remove_page() is executed in the main context as if g_main_context_invoke() is used? But what if I manually emit the signal with g_signal_emit() inside the thread (if the signal is emitable on that way)?

推荐答案

首先,gtk mainloop不是线程安全的.在UB中,可以从除mainloop之外的其他线程中调用任何gtk函数.

First of all, gtk mainloop is not thread safe. It's UB to call any gtk functions from threads other than mainloop.

可以在此处找到详细说明>,但总而言之:mainloop不会休眠或等待信号.它总是在迭代,如果看到该用户按下按钮,它将发出信号.

Detailed description can be found here, but in short: mainloop doesn't sleep or wait for signals. It's always iterating and if it sees, that user pressed a button, it emits a signal.

我个人使用的是g_timeout_add'ed这样的函数:

Personally I use functions of this kind, which are g_timeout_add'ed:

static gboolean
redrawer (gpointer data)
{
   MyObj *self = data;

   if (g_atomic_int_get (&self->priv->request_redraw)
     gtk_widget_queue_draw (GTK_WIDGET (self));

  return G_SOURCE_CONTINUE;
}

编辑:过了一会儿,我发现g_idle_add是线程安全的,因此可以在不显式检查request_redraw标志的情况下重写此函数:

Edit: a bit later I found out, that g_idle_add is thread-safe so this function could be rewritten without explicit checking of request_redraw flag:

static void
callback_which_initiates_redraw (gpointer data)
{
   MyObj *self = data;
   g_idle_add (gtk_widget_queue_draw, self);
}


更具体地说,它是异步发出的,以便我可以在某些函数中间调用信号,或者它等待特定函数先返回吗?

More specifically is it emitted asynchronously so that I can invoke the signal in the middle of some function or it waits for the particular function to return first?

等待.当您处于主循环中时,没有什么是异步的.

It waits. While you are in main loop, nothing is asyncronous.

发出切换页面"消息. GThread中的信号(例如,使用gtk_notebook_remove_page())可能会产生奇怪的效果,因为事件发生在主线程中,并且我无法保证gtk_notebook_remove_page()在主上下文中执行,就好像使用了g_main_context_invoke()一样? 但是,如果我在线程内部使用g_signal_emit()手动发出信号(如果该信号可以那样发出)怎么办?

Emitting the "switch-page" signal (for example with gtk_notebook_remove_page()) from within GThread may have odd effect as the events happen in the main thread and I cannot guarantee that gtk_notebook_remove_page() is executed in the main context as if g_main_context_invoke() is used? But what if I manually emit the signal with g_signal_emit() inside the thread (if the signal is emitable on that way)?

我不太理解这两个问题,但同样:UB是不是从mainloop调用gtk函数.

I don't quite understand these 2 questions, but again: calling gtk functions not from mainloop is UB.

这篇关于什么时候发出GTK信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆