View previous topic :: View next topic |
Author |
Message |
eldar
Joined: 14 Jun 2008 Posts: 101 Location: Ufa, Russia
|
Posted: Mon Jun 30, 2008 3:41 pm Post subject: Bug in gtk.Main.initMultiThread |
|
|
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 |
|
|
Mike Wey
Joined: 07 May 2007 Posts: 428
|
Posted: Tue Jul 01, 2008 1:01 pm Post subject: |
|
|
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 |
|
|
eldar
Joined: 14 Jun 2008 Posts: 101 Location: Ufa, Russia
|
Posted: Tue Jul 01, 2008 2:45 pm Post subject: |
|
|
I'm a real newby ) just tried to help as much as I could... |
|
Back to top |
|
|
|