我有一个使用pthreads创建的线程,我在该线程中接收进度通知回调。这是在外部非glib库中完成的。我希望回调发出一个glib信号事件,但我不确定如何将事件发送到主(GUI)线程。
库代码如下所示:
struct callback
{
virtual void onprogress(int progress) = 0;
virtual ~callback() {}
};
void perform_task_impl(callback *cb)
{
int progress = 0;
while (long_task_is_in_progress)
{
// perform task
progress += ...; // how much progress (not %) made
if (cb) cb->onprogress(progress);
}
}
void perform_task(callback *cb)
{
pthread_t threadId;
pthread_create(&threadId, NULL, perform_task_impl, cb);
}
GTK + / glib代码正在执行以下操作:
struct MyObjectPrivate : public callback
{
MyObject *self;
void onprogress(int progress)
{
// Q: how to send this to the main/GUI thread?
g_signal_emit(self, signals[PROGRESS], 0, progress);
}
};
void my_object_perform_task(MyObject *object)
{
perform_task(object->priv);
}