Multithreading problems -- with short example



I've attached a small .c file with code that has been given 
me problems (it's not the exact code in my program, but it
effectively and simply shows my problem)

I compile this with
gcc -o gtk-thread gtk-thread.c `gtk-config --libs` -lpthread

if I change the gtk_widget_show(window) and gtk_widget_hide(window)
to
gtk_widget_show(label) and gtk_widget_hide(label) it hides the label
and shows it without problems.  With the window, I move the mouse
over the window and boom window disappears but never comes back
as it's supposed to...

simply put, I can hide/show a label out of a thread, but not a window.

take a look at the code, and let me know what I'm doing wrong.

Thanks!


-- 
-----------------------------
Brad House
Sr. Developer
Main Street Softworks, Inc.

brad mainstreetsoftworks com
(352) 378-8228
-----------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <gtk/gtk.h>
#include <pthread.h>

GtkWidget *window;
GtkWidget *label;

void destroy(GtkWidget *widget, gpointer data)
{
  gtk_main_quit();
}

void *run_thread(void *args)
{
  int visible=1;
  while (1)   {
      sleep(1);
      gdk_threads_enter();
       if (visible) {
          visible=0;
          gtk_widget_hide(window);
        } else {
          visible=1;
          gtk_widget_show(window);
        }
       gdk_threads_leave();
   }
}

int main(int argc, char *argv[])
{
  pthread_t thread;

  /* init gtk */
  gtk_init(&argc, &argv);

    /* create a window */
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  gtk_signal_connect(GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC(destroy), NULL);
  gtk_container_set_border_width(GTK_CONTAINER (window), 10);

  /* create a label */
  label = gtk_label_new("This window should hide and show itself once a second...from a thread");
  gtk_container_add(GTK_CONTAINER(window), label);

  /* show everything */
  gtk_widget_show(label);
  gtk_widget_show (window);

  /* create the thread */
  pthread_create(&thread, NULL,(void *)run_thread, NULL);
  /* enter the GTK main loop */
  gdk_threads_enter();
  gtk_main();
  gdk_threads_leave();
  return(0);
}


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