How do I provide samples loaded in a DFD Thread, for lock-free reading on the Audio Thread?


I’m building a sampler and for my DFD streaming I have a DFDEngine object to pick the driest active voice, and fill up samples to, whenever a thread in it’s pool gets free.

So my problem is - how do I transfer the samples I’ve loaded in a DFD Thread to the Audio Thread, in such a way that no locking occurs while reading in the Audio Thread?


Use a FIFO, Look at AbstractFifo and create an audio-buffer FIFO class based on AbstractFifo


Thanks for the response @chkn.

I’ve just started reading a paper by Ross Bencina:


@chkn I am having trouble designing the communication between producer and consumer.

I understand how the producer -> consumer way will go, but how do I make a request from the consumer (voice) to the producer (DFDServer) (these being “I need samples” and “I am no longer active”)?

Here is a rough description of the design I am thinking of:

  • The producer (DFDServer) runs on a single background thread and it has a threadPool for serving multiple voices simultaneously. This technically means that the role of a “producer” is transferred to a threadPool job completing a read from the disk (it will write the samples to voice’s FIFO).
  • The DFDServer has an “inbox” for each voice (for the backward communication) and in this line of communication it becomes the “consumer”. This “inbox” is also implemented with AbstractFifo and there, a voice adds requests to the DFDServer.

But I can’t figure these out:

  • How do I implement the DFDServer’s continuous monitoring for new requests?
  • It will be running on a background thread, but how do I make it start working (almost) immediately after a voice has posted a request for samples, without putting much load on the CPU (infinitely looping through the voices)?
  • Is there a way to keep the DFDServer thread asleep and just wake it up when there is “mail”?

Any help is much appreciated.