Re: Threads and main-loops



On Thu, Sep 02, 2004 at 06:08:50PM +1000, Russell Shaw wrote:
John K Luebs wrote:
On Thu, Sep 02, 2004 at 04:56:26PM +1000, Russell Shaw wrote:

Hi,

If i make multiple loops with:

GMainLoop *loop1=g_main_loop_new(context1,TRUE);
GMainLoop *loop2=g_main_loop_new(context2,TRUE);
GMainLoop *loop3=g_main_loop_new(context3,TRUE);
g_main_loop_run(loop1);
g_main_loop_run(loop2);
g_main_loop_run(loop3);

Are all these running in a single gtk thread? If so, can their
callbacks call GUI widgets without thread-locking problems?

The simple answer to the question is: yes. g_main_loops know nothing
about threads (in otherwords they are meant to be "owned" or contained
by a process, they do not contain one) g_main_loop_run is simply a
function call that runs in the context of the thread that it is called
from and returns when it is finished.


What are you trying to accomplish, exactly?

A drawing program that is scriptable. Wondering how best to
use multiple main loops.

The main loop really doesn't do much more than just iterate its
associated content until it is told to quit. In the code above, loop1
will run until it is told to quit, then loop2 will run, then loop3 will
run only after loop2 quits. g_main_loop_run will not return control
until the loop ends. This is essentially like popping up three modal
dialog boxes in a row.

There is nothing that can be accomplished with the construct shown above
that couldn't be done with a single mainloop instance, which is why I
asked for clarification on what you were trying to do. Even though all 3
loops are created, context2 will never be serviced until the first loop
is done. 

Now if you were just illustrating, and actually planning on nesting the
g_main_loop_runs, then that might be a viable design. I was just curious
why you needed "mutiple mainloops."
My gut tells me that there are limited cases where you would want more
than one context per thread.

Have Fun,
 -jkl




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