Re: glib main loop concept



On 10/2/07, Christian Buennig <masala web de> wrote:
> * Do main loops exist per process or per thread?
>   If I start a main loop in one thread (A) and add an IO watch in
>   another thread (B), does this watch get added to the main loop of the
>   thread A or B ?

Each process can have one or more GMainContext. A context holds a set
of IO watches, timeouts, callbacks, etc.  There's a default context,
which is generally the only context. If you use the convenience
functions g_timeout_add() etc., they will automatically add to the
default context. You'll need to roll up your own timeout_add() (or
whatever) if you want to add to a different context.

Each context can have one or more main loops. These main loops are not
active at the same time, instead they nest in a stack with only the
top one active. GTK uses nested main loops to handle modal dialogs. I
imagine the context and all the main loops for that context have to be
created and run in a single thread, but I don't know. It would
certainly reduce confusion if they were.

So to answer your question, if you have a main loop running in thread
(A) and add an IO watch from thread (B), the callback on input
arriving will be called by (A). Unless you go to a lot of trouble to
add your watch to the non-default context.

> * If there is only one main loop (regardless of per thread or per
>   process), why the function g_main_loop_new()?

This is for the nesting feature.

> * What's the purpose of the parameter 'is_running' in
>   g_main_loop_new() ? When should I set it to TRUE and when to FALSE?

No idea :) I guess you should leave it FALSE and rely on
g_main_loop_run() to turn this on for you.

John



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