Re: Thread-save posting of events



On 3/29/07, Tobias Rapp <t rapp noa-audio com> wrote:
Rick Jones wrote:
> I've only come across it while looking for other things, but perhaps the
>  GAsyncQueues stuff would be useful for thread-to-thread comms?
>
> http://developer.gnome.org/doc/API/2.0/glib/glib-Asynchronous-Queues.html

That looks exactly like the thing I'm searching for. I had a look at the
API documentation before but don't know how I missed that one. Thanks!

> You probably still need watch routines executing the the various event
> loops (guessing).

Yes, maybe I have to add an idle handler for the event loop with
g_idle_add() (as proposed by John) and do a g_async_queue_try_pop() in
there...

Actually no, you can just use g_idle_add() like this:

 my_thread()
 {
   for(;;){
     // worker thread main loop
     Thing *stuff;

     stuff = g_new( Thing, 1 );
     calculate_thiing( stuff );
     g_idle_add( handle_stuff, stuff );
   }
 }

Then the function handle_stuff will be run by your program's main loop
thread when it's next idle:

 handle_stuff( Thing *stuff )
 {
   update_gui( stuff );
   g_free( stuff );
 }

You might need something to stop the worker filling RAM with
unprocessed packets of stuff if the GUI thread can't keep up with the
worker's rate of production.

GAsyncQueue can be used to communicate between two threads without
involving the main loop at all. If one of your threads is running the
GTK main loop, then g_idle_add() is far simpler.

John



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