Re: Multi-threading & portability



Jean-Yves Lefort wrote:

Hi all,

Please consider the attached C source.

It runs fine on FreeBSD, but freezes on Linux. gtk_dialog_run() calls
a recursive main loop, but I don't know how to interpret this fact to
solve my problem. The workaround I use actually is to simply remove
the gdk_threads_enter()/gdk_threads_leave() pair wrapping the main
loop, as the program runs fine without them.

Why should I wrap the main loop with that pair?
Is the attached program not correct? Did I mess up somewhere?

Hello,
This is the classical deadlock. The problem is that gtk_dialog_run has GDK_THREADS_LEAVE - GDK_THREADS_ENTER pair, not GDK_THREADS_ENTER - GDK_THREADS_LEAVE. When you call gtk_dialog_run inside the main loop - all is OK, but you will get locked in your situation, cause: 1. gtk_dialog_run calls GDK_THREADS_LEAVE -> g_mutex_unlock(), runs mail loop. 2. After main loop finished gtk_run_dialog calls GDK_THREADS_ENTER -> g_mutex_lock()
and owns gdk_threads_mutex mutex.
3. You code calls gdk_threads_enter and get the deadlock cause mutex is owned (under Linux pthread uses 'fast' mutexes, which causes to block even they were locked by the same thread).

If your application is not mt or you dont draw anything in another threads you can skip
gdk_main() guarding.

<skipped>

  g_signal_connect(G_OBJECT(window), "delete_event",
		   G_CALLBACK(delete_event_handler), NULL);

You can save some lines of code here, if you put: g_signal_connect(..., (GCallback)gtk_main_quit, ...) ;)
<skipped>

  dialog = gtk_message_dialog_new(GTK_WINDOW(window),
				  GTK_DIALOG_DESTROY_WITH_PARENT,
				  GTK_MESSAGE_WARNING,
				  GTK_BUTTONS_OK,
				  "Test dialog");

/* You need to put these lines to get it work under Linux: */
gdk_threads_enter(); /* you owns the mutex, this line can be ignored */

 gtk_dialog_run(GTK_DIALOG(dialog));

gdk_threads_leave(); /* you freeing the mutex, this line is what you need */


The behaviour under FreeBSD and Linux depends on threading library are you using.

Olexiy





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