Re: Gtk+ problem with multithreading



2009/5/3 patkhor <indyz-fluke hotmail com>:

Hi,

This is the first time I write GUI application using Gtk+ and I have some
problems
with multithreading.

What I want to achieve is that when the user clicks on OK button, the
program will call
callback_func which creates some number of threads (in this case, I use 5).
And each threads then start executing thread_func.

The problem I have is, when I click on OK button, the main window freezes
(display "Not responding"
message on Windows machine) until all the threads finish its execution.

The following is part of the code:

struct thread_data_t {
   Âint member1;
   Âint member2;
};

struct thread_data_t thread_data[5];

void *thread_func(void *threadarg) {
   Âstruct thread_data_t *thread_data;

   Âthread_data = (struct thread_data_t *) threadarg;

   Â.........................
   Â.........................

   Âreturn NULL;
}

void callback_func(GtkWidget *button, gpointer data) {
   ÂGThread *threads[5];
   Âint i;

   Â...................
   Â...................

   Âfor (i = 0; i < 5; i++)
       Âthreads[i] = g_thread_create((GThreadFunc)thread_func, (void
*)(&thread_data[i]), TRUE, &err[i]);

   Âfor (i = 0; i < 5; i++)
       Âg_thread_join(threads[i]);

   Â...................
   Â...................
}

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

   Âg_thread_init(NULL);
   Âgdk_threads_init();

   Âgtk_init(&argc, &argv);

   Â..................
   Â..................

   Âg_signal_connect(GTK_OBJECT(ok_button), "clicked",
GTK_SIGNAL_FUNC(callback_func), (gpointer *)&arg);

   Â..................
   Â..................

   Âgdk_threads_enter();
   Âgtk_main();
   Âgdk_threads_leave();

   Âreturn 0;
}

Any help would be highly appreciated,
Pat

PS. Sorry for my bad English. It's my second language.
--
View this message in context: 
http://www.nabble.com/Gtk%2B-problem-with-multithreading-tp23353368p23353368.html
Sent from the Gtk+ - Apps Dev mailing list archive at Nabble.com.

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Hello.

The problem you're experiencing is caused by the fact that you don't
return from callback function until all of the threads are joined. And
while you're inside the callback, main loop is blocked, which results
in unresponsive GUI.

You'll need to either make your threads unjoinable or join them in
some other function when they finish.

-- 
Tadej BorovÅak
tadeboro.blogspot.com
tadeboro gmail com
tadej borovsak gmail com



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