Re: handlers for g_timeout_add and g_idle_add different threads?



Al Hooton wrote:
        Quick question:  I've read through all the stuff I can find on the glib
main loop support for timeout handlers and idle handlers, but there's a
simple question I have:

- Do either, or both. of these types of handlers get run in a different
thread, or are they run out of the mainloop/GMainContext the
g_timeout_add and g_idle_add routines were called in to add them?

They are both run inside the mainloop, so in the same thread that called gtk_main(). This means that you shouldn't spend too much time in an idle or timeout handler because it will block your gui.

Note that adding idle and timeout handlers is thread safe and can be done from any thread without needing to acquire any locks first. I find g_idle_add to be the best way for a worker thread to communicate with the gui thread.

If you have called gdk_threads_init then idle and timeout handlers will be run *without* the gdk thread lock and you will have to add gdk_threads_enter/leave calls yourself if you are doing gui stuff.

--
Tim Evans
Applied Research Associates NZ
http://www.aranz.com/



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