gtk threads



Hi,
This test prog counts to 20 on the terminal window and should show
a GtkLabel "test" displayed in a GtkWindow.

With the thread code commented out as below, "test" is shown correct.
When the thread is allowed to run, the label is not shown. Why so?


#include<glib.h>
#include<glib/gprintf.h>
#include<gtk/gtk.h>
#include<gtk/gtksignal.h>

gboolean
on_destroy(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
    gtk_main_quit();
    g_printf("quit\n");
    return FALSE;
}

GAsyncQueue *command_queue;

static gpointer
command_thread(gpointer data)
{
    gint i;
    for(i=0;i<20;i++){
        g_async_queue_push(command_queue,&i);
        g_usleep(G_USEC_PER_SEC*0.1);
    }
    return NULL;
}

static gboolean
events_prepare(GSource *source, gint *timeout)
{
    return TRUE;
}

static gboolean
source_cb(gpointer data)
{
    g_printf("i:%d\n",*((gint*)data));
    return TRUE;
}

static gboolean
events_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
{
    gint *pi;
    if((pi=g_async_queue_try_pop(command_queue))!=NULL)
        callback(pi);
    return TRUE;
}

static GSourceFuncs events_funcs={
    events_prepare,
    NULL,
    events_dispatch,
    NULL
};

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

    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    GtkWidget *label=gtk_label_new("test");

    gtk_container_add(GTK_CONTAINER(window),label);

    gtk_widget_show_all(window);

    g_signal_connect(window,"delete-event",G_CALLBACK(on_destroy),NULL);
    g_signal_connect(window,"destroy-event",G_CALLBACK(on_destroy),NULL);

/*******  following code stops "test" label appearing

    command_queue=g_async_queue_new();

    g_thread_create(command_thread,NULL,FALSE,NULL);

    GSource *source=g_source_new(&events_funcs,sizeof(GSource));

    g_source_set_callback(source,source_cb,NULL,NULL);

    g_source_attach(source,NULL);

*********/
    gtk_main();

    return 0;
}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]