g_timeout_add_full from different thread not working



Calling g_timeout_add_full() from a different thread (note the pthread_create in the sample bellow) than 
where gtk_main() is does not work as I expect.

The timeout function only gets called if I generate events by moving the mouse.  If I don't move the mouse, 
all I see is the timeout is added repeatedly, but timeout_func does not get called.

I'm afraid there's something I do not understand... and thus, your help would be appreciated.

#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>

gboolean
timeout_func (gpointer data)
{
  static int cnt=0;
  cnt ++;
  printf("%s %d %p\n", __func__, cnt, (void*)data);
  return FALSE;
}

void *
thread_func (void*arg)
{
  printf("%s()\n", __func__);
  g_timeout_add_full(G_PRIORITY_DEFAULT, 800, timeout_func, 1, NULL);
  while (1) {
    sleep(2);
    printf("%s() add\n", __func__);
    g_timeout_add_full(G_PRIORITY_DEFAULT, 800, timeout_func, 2, NULL);
  }
}

int
main (int argc, char **argv)
{
  pthread_t t;
  gtk_init(&argc, &argv);
  GtkWidget *w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(w), "Hello");
  gtk_widget_show_all(w);
  pthread_create(&t, NULL, thread_func, NULL);
  gtk_main();
  return 0;
}



      __________________________________________________________________
Looking for the perfect gift? Give the gift of Flickr! 

http://www.flickr.com/gift/



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