Re: Quitting a child thread's GMainLoop



On Wed, 4 Aug 2010 23:33:19 +0100
Chris Vine <chris cvine freeserve co uk> wrote:
> On Wed, 04 Aug 2010 17:50:42 -0400
> Tony Cebzanov <tonycpsu gmail com> wrote:
> 
> > 
> > I'm developing a multi-threaded GLib application.  Each thread,
> > including the main thread, has its own GMainLoop for polling input
> > sources.
> > 
> > This works well, but I'm having trouble finding an elegant way to
> > shutdown the child threads from the main thread, because the "main
> > loops" I want to quit live in the child threads.  Is there any way I
> > can quit the main loop of a child thread from a parent thread?  Can
> > a parent thread somehow access the "main loop context" of a child
> > thread and tell it to quit somehow?
> > 
> > I know I can pass in data (such as the address of a gboolean
> > "shutdown" variable) to the g_thread_create call(), but how does the
> > child thread check this flag when running in a GMainLoop?  It seems
> > a bit silly and wasteful to create an idle GSource just to check a
> > quit flag -- but is that the only way to do it?
> 
> I have probably not understood what you are trying to do, but if all
> the threads have their own main loops why not have the main thread
> pass an idle callback to the thread(s) you want to end which calls
> g_main_loop_quit()/gtk_main_quit()? If you have called
> g_thread_init() then the main loops will all be thread safe.

That would of course be g_main_loop_quit(), not gtk_main_quit(), since
you want a worker thread to terminate.  Actually, since glib main loops
are thread safe if you have called g_thread_init(), quite possibly in
the main thread you can call q_main_loop_quit() directly on the worker
threads' main loops, without using an idle callback.  I would want to
have a look at the glib code for g_main_loop_quit() first though.

Chris


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