Re: How to oblige Gtk to redraw the window



Paul Davis <pbd Op Net> writes:

> >In fact, i'm using fork and not threads. My progress
> >bar indicates the percentage done while reading the
> >file. Is it better to use threads?
> 
> read the FAQ.
> 
> threads are more lightweight, plus you cannot use fork() this way
> since you cannot use mutexes between processes; GTK (and X) requires
> are not thread-safe without the mutexes being acquired and released.

A Gtk+ application that needs to access the ui from more than one
thread should most likely just do

   gtk_idle_add (function_that_updates_the_gui, whatever_data_it_needs)

where the function_that_updates_the_gui returns FALSE, so that it is
only called once.  This function can safely access all parts of the
gui, because it is run from the same thread that ran gtk_main().  The
function gtk_idle_add() can be called without any locking.

Also note that if you need to react to something that happens
externally, you are often better off creating a new event source for
the mainloop instead of creating a new thread.

If you can avoid spawning threads, do it. GLib's mainloop is very
capable - if you use it, your application will often be simpler to
write and understand - and also be faster - than if you use threads.




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