Re: multithreading on win32



Christophe Combelles wrote:
It has never been impossible to control the GUI from a thread -- as a matter of fact, the GUI is controlled by a thread itself :). Now, if you are talking about having multiple threads accessing the GUI data structures, you will have
to provide the synchronization yourself.

   gdk_threads_enter() and gdk_threads_leave() are your friends.

There are several apps that do that: Gaim and Urlget are 2 that comes to
mind.

Hope this helps.
Harring Figueiredo


I would like to something like that (see below):
But it doesn't work. The window disappears immediatly when the thread finishes,
and if the thread is kept alive (uncomment "sleep"), the window is frozen.
I just need to create a thread, and that thread must notify the user of its activity. (at least when it starts and stop, or with a progress bar)

-----------test.c--------------------
#include <gtk/gtk.h>

GtkWidget *window;
GtkWidget *label;
GThread *thread;

gint function() {
  gdk_threads_enter();
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  label=gtk_label_new("my text");
  gtk_container_add(GTK_CONTAINER(window), label);
  gtk_widget_realize(window);
  gtk_widget_show_all(window);
  gdk_threads_leave();
  //sleep(10000);
}
gint main(int argc, char *argv[]) {
  g_thread_init (NULL);
  gdk_threads_init();
  gtk_init(&argc,&argv);
  thread = g_thread_create((GThreadFunc)function, NULL, TRUE, NULL);
  g_thread_join(thread);
  gdk_threads_enter();
  gtk_main();
  gdk_threads_leave();
}
--------------------------------------------
compiled on MinGW with:
gcc -g -DDEBUG -mms-bitfields -mwindows `pkg-config --cflags gtk+-2.0` -c test.c
-o test.od
gcc -static -mms-bitfields -mwindows -o test.exe test.od `pkg-config --libs g
tk+-2.0 --libs gthread-2.0` -L/lib
--------------------------------------------

Hi,
        I think the program you posted above just needs to define a main loop
for the thread.  Like the following:

gint function()
{
    GMainLoop *main_loop;
>   gdk_threads_enter();
>   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
>   label=gtk_label_new("my text");
>   gtk_container_add(GTK_CONTAINER(window), label);
>   gtk_widget_realize(window);
>   gtk_widget_show_all(window);
>   gdk_threads_leave();
>   //sleep(10000);
    main_loop = g_main_loop_new( NULL, TRUE );
    g_main_loop_run( main_loop );
}

this means this function will block. But then that's ok you can communicate
between threads using async queues, time out functions, or regular signal handlers.

hope this helps.

-todd



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