【问题标题】:C and GTK3 - How to run a thread uninterrupted?C 和 GTK3 - 如何不间断地运行线程?
【发布时间】:2021-09-17 00:23:07
【问题描述】:

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

但程序在少量“滚动”后崩溃。有时它甚至没有通过第一卷。会产生各种运行时错误。

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

程序在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 拥有您所需要的一切
  • 顺便说一下,您的互斥锁没有任何保护,因为它仅用于 1 个线程。它不会阻止不同的线程访问相同的数据
  • 1) 我更新了问题以删除互斥代码。 2)我查看了“this”链接。但它似乎涵盖了 >= 1 秒的时间间隔。 3)我做了一个循环,除了占用时间之外什么都不做。但是代码仍然崩溃。所以我不确定是否是睡眠问题。

标签: c thread-safety mutex gtk3


【解决方案1】:

正如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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多