Re: [gtk-list] Re: gdk_thread_leave() in call back func, otherwisedead lock.



From: Owen Taylor <otaylor@redhat.com>
Subject: [gtk-list] Re: gdk_thread_leave() in call back func, otherwise dead lock.
Date: 18 Jan 2000 08:39:15 -0500

> 
> Sebastian Wilhelmi <wilhelmi@ira.uka.de> writes:
> 
> > Hi Yasushi,
> 
> > > My question is:
> > > 
> > > - Is it ok to call gdk_thread_leave() in call-back function, if the
> > >   function does not call any gtk functions?
> >
> > Yes, thats perfectly fine, but as long as you're blocking inside a
> > callback (and that is what is happening here), you have an
> > unresponding interface. It might be better to just set loop_going
> > to 0 and let loop_thread() notify the main thread via a gtk signal
> > as the last command it executes before it closes down [...]
>
> Just to clarify - signals are always executed in the current thread.
> To notify the main thread, you'd have to use a different mechanism.
> The most convenient one is probably g_idle_add() - the idle you
> add will get executed in the main thread.

I fixed previous code the way I think Owen meant in his previous
post, but not sure I'm going to the right direction.  Could someone
check it?

regards,
--
           yashi

-------- 8< ------ cut -------- 8< ----------

/* -*- compile-command: "gcc -Wall -g thread.c `gtk-config --cflags --libs gthread`" -*- */
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <gtk/gtk.h>

pthread_t thread;
int loop_going;
GtkWidget *window;

int loop_thread_join(gpointer data){
	pthread_join(thread, NULL);
	gtk_main_quit();
	return 0;
}

void *loop_thread(void *arg){
	while(loop_going){
		sleep(1);
		GDK_THREADS_ENTER();
		gtk_widget_draw(window, NULL);
		GDK_THREADS_LEAVE();
	}
	g_idle_add(loop_thread_join, NULL);
	return NULL;
}

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

void quit(GtkWidget *widget, gpointer data){
	loop_going = 0;
}

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();

	return 0;
}



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