C 和 GTK3 - 如何 运行 一个线程不间断?

C and GTK3 - How to run a thread uninterrupted?

我一直在尝试:显示 4 个随机的骰子面,相隔 0.4 秒,给人一种骰子正在滚动的印象。我已将执行此操作的代码放在线程中。

但程序在少量“滚动”后崩溃。有时甚至连第一卷都过不去。生成各种 运行 时间错误。

我最后尝试的是线程代码的互斥保护。我怀疑这只是保护代码免受其他线程的影响,但我的程序没有其他线程。

该程序 运行ning 在 Linux 下,编译时使用:gcc -rdynamic -o test3d1 test3d1.c pkg-config --cflags --libs gtk+-3.0

#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>

GtkWidget *window;
GtkWidget *vbox;
GtkWidget *butt;
GtkWidget *image;
GdkPixbuf *pixbuf1;
GdkPixbuf *pixbuf2;
GdkPixbuf *pixbuf3;
GdkPixbuf *pixbuf4;
GdkPixbuf *pixbuf5;
GdkPixbuf *pixbuf6;

pthread_t thread;

void *fn_thread ()
{
    gtk_widget_set_sensitive (butt, FALSE);

    int i;
    for(i = 1; i < 5; i++)
    {
        int j;
        j = rand () % 6 + 1;
        switch (j)
        {
            case 1: gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf1); break;
            case 2: gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf2); break;
            case 3: gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf3); break;
            case 4: gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf4); break;
            case 5: gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf5); break;
            default: gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf6);
        }
        usleep (400000);
    }

    gtk_widget_set_sensitive (butt, TRUE);

    return NULL;
}

void startthread ()
{
    pthread_create (&thread, NULL, fn_thread, NULL);
}

int main (int argc, char *argv[])
{
    gtk_init (&argc, &argv);

    srand (time(0));

    pixbuf1 = gdk_pixbuf_new_from_file ("dice_1_48x48.png", NULL);
    pixbuf2 = gdk_pixbuf_new_from_file ("dice_2_48x48.png", NULL);
    pixbuf3 = gdk_pixbuf_new_from_file ("dice_3_48x48.png", NULL);
    pixbuf4 = gdk_pixbuf_new_from_file ("dice_4_48x48.png", NULL);
    pixbuf5 = gdk_pixbuf_new_from_file ("dice_5_48x48.png", NULL);
    pixbuf6 = gdk_pixbuf_new_from_file ("dice_6_48x48.png", NULL);

    image = gtk_image_new ();
    gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf1);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
    gtk_window_set_default_size (GTK_WINDOW (window), 150, 100);

    butt = gtk_button_new_with_label ("Roll");
    g_signal_connect (G_OBJECT (butt), "clicked", G_CALLBACK (startthread), NULL);

    vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL,10);
    gtk_box_pack_start (GTK_BOX (vbox),image,0,0,0);
    gtk_box_pack_start (GTK_BOX (vbox),butt,0,0,0);
    gtk_container_add (GTK_CONTAINER (window), vbox);
    gtk_widget_show_all (window);

    gtk_main ();

    pthread_join (thread, NULL);

    return 0;
}

正如 this 回答中所指出的,除了执行 gtk_main 的线程之外,您不应该从任何其他线程调用 GTK 函数。

事实上,在 event-driven program 中你根本不应该使用额外的线程,除非你需要优化一些繁重的计算。你应该做的是利用你的事件驱动框架的特性,在你的例子中是 GTK,以便在主线程中执行所有事件处理。

在你的情况下,这意味着你应该使用 g_timeout_add 函数来安排定期从主线程调用你的动画函数:

gboolean fn_roll_dice (gpointer)
{
    gtk_widget_set_sensitive (butt, FALSE);

    int j = rand () % 6 + 1;
    switch (j)
    {
        case 1: gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf1); break;
        case 2: gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf2); break;
        case 3: gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf3); break;
        case 4: gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf4); break;
        case 5: gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf5); break;
        default: gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf6);
    }

    gtk_widget_set_sensitive (butt, TRUE);

    return TRUE;
}
    
int main (int argc, char *argv[])
{
    ...
    g_timeout_add (400, fn_roll_dice, NULL);

    gtk_main ();

    return 0;
}