GTK+/GLib thread behaviour



Hi,

I'm struggling with a problem in my application that seems to be thread
related. I'm developing and application, that has real time video
acquisition, processing and visualisation functionality. When video
acquisition button is clicked I call function:

StartVideo( callback_fcn( *image), data )

Afterwards every time there is a new image available function `callback_fcn(
*image )` is called, and `*image` will point to a buffer containing the new
image. `StartVideo` and the image buffer are provided by the camera API.

In function `callback_fcn( *image)` i do the following:

1. Copy data located at *image to my own ImageBuffer
2. Perform various calculations with the ImageBuffer
3. Obtain a lock on GDK using gdk_threads_enter()
4. Copy the processed data to a separate FrameBuffer
5. Draw the FrameBuffer onto a GtkDrawingArea (the GtkDrawingArea is redrawn
via a expose-event-handler from the FrameBuffer as required)
6. Release GDK lock using gdk_threads_enter()
7. Return from function

My first trouble was (it is kind of solved now, and presented as a reference
and a sanity check only) that, depending on load, the application would
crash and the GtkDrawingArea is to blame. This was because the expose-event
handler function of the GtkDrawingArea would use data from the FrameBuffer
and on a sepparate thread it would be simultaniously overwritten in step 4.
Adding steps 3 and 6 solved this and the application was fairly stable for
extended periods of time.

However, since then the amount of workload was increased and now again the
application is experiencing erratic hookups and crashes. Debugging shows
that new threads of `callback_fcn( *image)` are starting before old ones are
done - that is the application can't cope with the video stream input. When
one thread is in step 2, another starts at step 1, and again same
ImageBuffer is used in two different threads.

I've tried locking the ImageBuffer using G_LOCK to no avail. The FAQ gives a
good example on thread usage, but I can't understand it completely. Maybe
someone could point out a good introductory text with code examples on GTK+
and GLib threading?

I understand that I'm pushing above the limits of my hardware if that sort
of things are happening, but I'd like to have some control over this (skip
frames or processing) rather than just don't do it. And since I don't have
any control over the `StartVideo` routine I'm out of ideas. I would rather
have no threads in my application at all, but I guess it's a better way (or
the only way, when using callbacks from a shared library functions). Now I
need to block new threads of `callback_fcn` when one thread is already
inside this function or put the calls in a queue, and drop frames or reduce
processing on the fly, but I can't figure out how to do this on my own.

Any help would be much appreciated.



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