FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Bug in gtk.Main.initMultiThread

 
Post new topic   Reply to topic     Forum Index -> gtkD
View previous topic :: View next topic  
Author Message
eldar



Joined: 14 Jun 2008
Posts: 101
Location: Ufa, Russia

PostPosted: Mon Jun 30, 2008 3:41 pm    Post subject: Bug in gtk.Main.initMultiThread Reply with quote

found a small bug in multithread suport - for example in demos/gtkd/TestWindow.d when initialising multithread support:
Code:

   version(Win32)
   {
      // todo threads are still broken on windows...
      Main.init(args);
   }
   else version(Tango)
   {
      Main.init(args);
   }
   else
   {
      Main.initMultiThread(args);
   }

There is no support for Win32. Corresponding to http://developer.gimp.org/api/2.0/gdk/gdk-Threads.html there is a small example :
Code:

int
main (int argc, char *argv[])
{
  GtkWidget *window;

  g_thread_init (NULL);
  gdk_threads_init ();
  gdk_threads_enter ();

  gtk_init (&argc, &argv);

  window = create_window ();
  gtk_widget_show (window);

  gtk_main ();
  gdk_threads_leave ();

  return 0;
}

we forgot to call gdk_threads_enter () in gtk.Main:
Code:

   public static void initMultiThread(string[] args)
   {
      Thread.init(null);
      gdkThreadsInit();
      init(args);
   }

Actually gdk_threads_enter () is wrapped - gdkThreadsEnter(); So Main.initMultiThread() should be:
Code:

   public static void initMultiThread(string[] args)
   {
      Thread.init(null);
      gdkThreadsInit();
      gdkThreadsEnter();
      init(args);

I checked the TestWindow with this modification - it started working.
That's not all - we should not forget to call gdk_threads_leave () like in the example above - I'm not good in D - so we can figure out together where to call this function, maybe in destructor of the Main class - of course with checking if gdk_threads_enter () was called.

P.S. I don't know the reason why it worked on linux - but I compiled the C-exampe above without gdk_threads_enter () - the program runs succesfully. So it's not about gtkD.
Back to top
View user's profile Send private message
Mike Wey



Joined: 07 May 2007
Posts: 428

PostPosted: Tue Jul 01, 2008 1:01 pm    Post subject: Reply with quote

gdkThreadsEnter / gdkThreadsLeave should be called before / after calls to gdk/gtk to acquire a lock for the thread you are calling the gdk/gtk funtions from. By putting the gdkThreadsEnter in initMultiThread you would only be able to make calls from the main threat untill you call gdkThreadsLeave.

the TestWindow demo does seem to miss a gdkThreadsEnter and gdkThreadsLeave around Main.run();

for the gdkThreadsEnter / gdkThreadsLeave you can find them on line 943 / 947 of TestWindow.d

for more info see: http://library.gnome.org/devel/gtk-faq/stable/x482.html

Also for Windows you can find the following in the documentation:

Quote:
GTK+ is thread aware but not thread safe - it provides a global lock controlled by gdk_threads_enter() / gdk_threads_leave() which protects all use of GTK+.That is, only one thread can use GTK+ at any given time.

Unfortunately the above holds with the X11 backend only. With the Win32 backend, GDK calls should not be attempted from multiple threads at all.


from the description section on: http://library.gnome.org/devel/gdk/unstable/gdk-Threads.html
Back to top
View user's profile Send private message
eldar



Joined: 14 Jun 2008
Posts: 101
Location: Ufa, Russia

PostPosted: Tue Jul 01, 2008 2:45 pm    Post subject: Reply with quote

I'm a real newby ) just tried to help as much as I could...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> gtkD All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group