Re: main loop & asynchronous queues

On Wed, 23 Sep 2009 08:44:06 -0500
Thomas Stover <thomas wsinnovations com> wrote:

Chris Vine wrote:
In the case of callbacks to execute in the default program
loop/context (the "main" main loop) a worker thread can use
g_idle_add()/ g_idle_add_full().  To execute a callback in other
threads' main loops you will have to do it in long-hand, making a
GSource object with g_idle_source_new(), and attach it to the
GMainContext for the thread in question (it is relatively trivial).
Wow that might open up some doors, but I've got to understand this 
first. I'm fuzzy on several things. Here is some stabbing in the dark:


To be honest I am not entirely clear from your code snippet what you are
trying to do, but the link I sent in my last post explains how to do it.

In your code you didn't create a GMainContext object for each of your
threads, which is the key point. Each thread in which you want a main
loop should call g_main_context_new() which will create a GMainContext
object for the calling thread, then use that to create a GMainLoop
object, then call g_main_loop_run() on it to run it.

You can then post to that main loop from any thread using an idle
source. If you want to see an example of how to set up an idle source,
look at the implementation of g_idle_add_full().  You can do the same,
except pass the GMainContext object for the receiving thread as the last
argument of g_source_attach() instead of NULL.

However, if all you want is to have a worker thread which will execute
tasks sent to it, then you might as well just block on a GAsyncQueue
object in a perpetual loop and put callbacks in the queue for execution.
Or thread pools may suit what you are doing better (see GThreadPool).


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