On Tue, Aug 02, 2005 at 07:47:32AM -0400, Owen Taylor wrote:
> On Sun, 2005-07-31 at 15:44 -0600, Pat Mahoney wrote:
> > (not subscribed; please CC replies)
> > 
> > I'm trying to find a way to provide my mainloop with asynchronous data
> > from a thread.  I have read this:
> > 
> > http://mail.gnome.org/archives/gtk-app-devel-list/2004-July/msg00181.html
> > 
> > which describes how to use a GAsyncQueue and GSource to watch the queue
> > for available data (and to have the thread wake up the mainloop when data
> > is available).  A callback is run in the context of gtk_main when
> > data is available.  It works perfectly except for one show-stopper
> > problem:
> 
> Note that using a custom GSource here is perhaps overcomplicated. The
> simplest method is:
> 
>  g_idle_add (function_to_call_in_main_thread, data_to_pass_to_function);
>  
> (Or use g_idle_add_full() if you want to prioritize the callback higher
> than the default idle priority.)

Yes, that is simplest.  In my case, the CPU usage jumps to 100% (though
the app is still responsive).  My particular app is reading frames from
one or more webcams (select() doesn't work with video4linux devices, so
this must be done in a seperate thread or process).  The camera_thread
is triggered by the mainloop to snap a picture which it pushes onto a
GAsyncQueue.

My callback on_camera_frame_available() checks the queue and returns
immediately if there is no data.  Similarly, the GSourceFuncs check the
queue.  However, by using the GSource, the check is performed about 2-3
times per frame (using printf() to watch that, so timing may be skewed),
while the g_idle_add() method runs my callback continually, and so the
GAsyncQueue is constantly being checked for data, sucking down the CPU.

-- 
Pat Mahoney     <[EMAIL PROTECTED]>
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Reply via email to