Re: Providing gtk_main() with asynchronous data



On Tue, 2005-08-02 at 06:36 -0600, Pat Mahoney wrote:
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.

Your idle function presumably has the wrong return value. To quote:

http://developer.gnome.org/doc/API/2.0/glib/glib-The-Main-Event-Loop.html#g-idle-add

  If the function returns FALSE it is automatically removed from the list of 
  event sources and will not be called again.

Regards,
                                                        Owen



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