[LAD] Best practice for sharing complex data structures with a jack callback function

Fons Adriaensen fons at kokkinizita.net
Sat Jul 7 00:34:35 UTC 2007


On Fri, Jul 06, 2007 at 06:48:54PM -0500, Andres Cabrera wrote:

> The long title says it all... =) I want to pass some data generated
> from real-time audio analysis to the main program (only in that
> direction, i.e. only the jack callback writes to the structures). Are
> there synchronization issues involved? Can I use a C++ object for this, 
> or is that an absolute no,no?

You can use a C++ object for this (it's the cleanest way IMHO),
but there are some points to watch out for.

* Within the callback you cannot safely allocate and object.
  It must exist statically, or be allocated at a safe place,
  and pointer given to the callback.

* The main program has to know when it should read the object.
  Either use a flag in the object, and let the main program
  poll it periodically (if the delay is acceptable), or use
  a semaphore. POSIX semas are simple and easy to use, and
  you can signal them safely from within the callback.

* The callback has to know when it can (re)write the object.
  Clearly it should never wait on a sema, and it is already
  periodic, so here you can use a flag in the object. 

* If you use a flag and polling in both directions make
  sure to use two separate flags. One is read-only for
  the main program and write-only for the callback and
  vice versa.

Things get a bit (but not much) more complex if you need
more than one object, but this should get you started.

-- 
FA

Follie! Follie! Delirio vano è questo !





More information about the Linux-audio-dev mailing list