gdk_thread_leave() in call back func, otherwise dead lock.



Hi all,

the following code will likely deadlocked, if I don't call
gdk_thread_leave() before pthread_join(), because: 

from gtkfaq-5.html#ss5.2
> Callbacks from GTK+ (signals) are made within the GTK+ lock.

deadlock happnes:

- loop_thread is waiting on sleep()
- button is clicked and GDK_THREADS_ENTER() is called in
  gdk_event_dispatch() for callback function.
- callback function calls pthread_join() and block itself to wait
  loop_thread.
- loop_thread leaves sleep() and trys to GDK_THREADS_ENTER() but it's
  already locked by callback function and block itself
- DEADLOCK!


My question is:

- Is it ok to call gdk_thread_leave() in call-back function, if the
  function does not call any gtk functions?

- if not, what would be the bast way to join threads within call back
  function?

thanks in advance.
regards,
--
          yashi

------- 8< -------- cut --------- 8< ---------
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <gtk/gtk.h>

pthread_t thread;
int loop_going;
GtkWidget *window;

void *loop_thread(void *arg){
	while(loop_going){
		sleep(1);
		GDK_THREADS_ENTER(); /* deadlock happens here */
		gtk_widget_draw(window, NULL);/* nothing */
		GDK_THREADS_LEAVE();
	}
	return NULL;
}

void loop_thread_start(){
	loop_going = 1;
	pthread_create(&thread, NULL, loop_thread, NULL);
}

void loop_thread_stop(){
	loop_going = 0;
	pthread_join(thread, NULL);
}

void quit(GtkWidget *widget, gpointer data){
/*	GDK_THREADS_LEAVE();*/ /* uncomment these to work around the deadlock*/
	loop_thread_stop();
/*	GDK_THREADS_ENTER();*/
	gtk_main_quit();
}

int main(int argc, char **argv){
	GtkWidget *button;

	g_thread_init(NULL);
	gtk_init(&argc, &argv);

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	button = gtk_button_new_with_label("close");
	gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(quit), NULL);
	gtk_container_add(GTK_CONTAINER(window), button);
	gtk_widget_show_all(window);

	loop_thread_start();
	GDK_THREADS_ENTER();
	gtk_main();
	GDK_THREADS_LEAVE();
}



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