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

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]