GTK + 3多线程 [英] GTK+3 multithreading

查看:341
本文介绍了GTK + 3多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序(用C写的,目前在Mac OS X 10.8.4中运行)每秒模拟世界和更新全球国家多次。

I have a program (written in C, currently running on Mac OS X 10.8.4) that simulates a world and updates the world's state multiple times per second.

在启动该程序创建它创建了一个简单的GTK窗口,并调用进入主循环

At startup this program creates a background thread which creates a simple GTK window and calls gtk_main.

世界上每个国家被更新(在主线程)的时间后,我想窗口反映这种变化。我的第一个方法是世界上被更新后,简单地更新GTK构件,而是因为这是在不同的线程的事情打破了相当乱七八糟。

After each time the world's state is updated (in the main thread), I would like the window to reflect that change. My first approach was to simply update the GTK widgets after the world was updated, but because that was in a different thread things broke quite messily.

是否有某种机制,其中主线程可以在图形线程更新一些状态,然后排队,提示图形线程重绘的事件吗?即。

Is there some sort of mechanism where the main thread can update some state on the graphics thread and then queue an event that prompts the graphics thread to redraw? I.e.

void draw() {
    // This can only be called from the graphics thread.
    gtk_label_set(GTK_LABEL(label1), "some state");
}

// This causes draw() to be called on the graphics thread.
gtk_please_redraw_this_thing_on_the_graphics_thread();

有没有办法做到这一点?或者说,它覆盖任何教程?

Is there any way to do this? Or any tutorials that cover it?

推荐答案

原来,这是非常简单的。

Turns out this is quite simple.

首先创建一个会做功能绘图(在图形螺纹):

First create the function that will do the drawing (on the graphics thread):

gboolean draw_function(GtkWidget *w, GdkEventExpose *event) {
    // Draw things.
    return TRUE;
}

然后,在建立,小部件的事件连接到您的绘制函数:

Then, during set up, connect the draw event of your widget to your draw function:

g_signal_connect(G_OBJECT(some_widget), "draw", G_CALLBACK(draw_function),  NULL);

最后,迫使小部件从另一个线程,你可以打电话重绘:

Finally, to force the widget to redraw from another thread you can call:

gtk_widget_queue_draw(some_widget);

这篇关于GTK + 3多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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