Problems with multithreading.



Hi!

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.


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:



#include <stdlib.h>
#include <stdio.h>
#include "threads.hh"

#include <gtk/gtk.h>


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

int
main (int argc, char **argv)
{
  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();

  Threads::thread tid;
  Threads::create(tid, the_ui_thread, NULL);



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

  gtk_main ();
  gdk_threads_leave();

  return 0;
}


(Unfortunately I cannot give a stacktrace, because the program doesn't
abort.
it just exits with code 01, and the error message.)

Any ideas?


	Xavier.



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