Re: [gtk-list] Problems with multithreading.



Hi, Xavier

> This is not my first program using gtk with multithreading, but I'm
> facing a strange problem.
> 
> I would like to run my gtk interface from a thread (which is not the
> main one).
> 
> Usually, I create the interface at the begin of the program, before
> creating any threads (and then I access it either from the main threads
> or others) and everything goes fine.
> 
> I want to make a program that creates a thread for the UI. So my idea
> was to make all the gtk_ calls in this thread (even the gtk_init one)
> and thus I was hopping that gtk wouldn't even need to be aware of runing
> in multithreading.
> But it doesn't work.
> 
> I get the following error for whatever X call taking place in the new
> thread:
> Gdk-ERROR **: Fatal IO error 11 (Resource temporarily unavailable) on X
> server curlew.fen.bris.ac.uk:0.0.

No idea, what that is coming from.
 
> I tried several combinations, such as all the initialisation in the main
> thread (and also the call to gtk_main) , and then creating the windows
> from the new thread, but that doesn't work either.
> 
> Is there any reason why every call to X not comming from the main thread
> should fail?
> Or maybe I made a stupid mistake I am too dumb to see?
> 
> (BTW, this is on solaris 2.6 or 2.5)
> 
> Here is simplified version of my program that does crash:

I've changed that prog to use pthreads instead of whatever thread.hh might be
and it works here on solaris and linux , even though the
gdk_threads_enter/leave() pair in the main thread prevents the window to be
displayed in the time the main thread is waiting, what you presumably
intended. Other than that it does, what it should.

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

#include <gtk/gtk.h>


void *the_ui_thread(void *ptr) 
{
  printf("hello\n");
  
  gdk_threads_enter();
  printf("creating the window\n");
  gtk_widget_show (gtk_window_new (GTK_WINDOW_TOPLEVEL));
  printf("ok.\n");
  gdk_threads_leave();
}

int
main (int argc, char **argv)
{
  pthread_t tid;
  g_thread_init(NULL);

  /* initialise gtk */
  //  gtk_set_locale ();
  gtk_init (&argc, &argv);
  
  //  add_pixmap_directory (PACKAGE_DATA_DIR "/pixmaps");
  //  add_pixmap_directory (PACKAGE_SOURCE_DIR "/pixmaps");

  gdk_threads_enter();
  pthread_create(tid, NULL, the_ui_thread, NULL);

  printf("zzzz...\n");
  sleep(2);
  printf("Now!\n");

  gtk_main ();
  gdk_threads_leave();

  return 0;
}

Bye,
Sebastian
-- 
Sebastian Wilhelmi                   |            här ovanför alla molnen
mailto:wilhelmi@ira.uka.de           |      är himmlen så förunerligt blå
http://goethe.ira.uka.de/~wilhelmi   |



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