[PD-dev] how to not cause audio drops with threads

Tom Schouten doelie at zzz.kotnet.org
Fri Sep 10 04:18:06 CEST 2004


hi all,

i was thinking about adding some non-blocking message
queue mechanism to pd, to simplify writing externals
with their own worker threads that cannot, by design,
block the audio thread.

i've found the need for this in almost all of my pd
projects that use threads, and the only one that has
solved it somewhat properly is wvvw.. adding support
to pd would prevent a lot of hassle. it could serve as
an 'external design pattern' actually.

this could be implemented in the form of a pd-message
queue. even a simple fixed size ringbuffer containing fixed 
size pd atoms would be a very nice thing to have as a standard 
part of pd. it doesn't require platform-specific code,
and doesn't use dynamic memory allocation for each message.

this can catch most of the communication needed between the 
main pd thread and a bunch of isolated worker threads, and do 
it in a way that forces you to not cause drops.

something like:

typedef struct {
	t_atom *buffer;
	int length;  // single message length in atoms
	int size;    // total number of messages
	int read;    // read atom index
	int write;   // write atom index
} t_queue;

t_atom *queue_read(t_queue *q);         // returns NULL if queue empty
int queue_write(t_queue *q, t_atom *a); // returns error code if full
void queue_free(t_queue *q);
t_queue *queue_new(int length, int size);



another thing that i've been looking for, is a standard 
way to hook into the main scheduler loop to add a poll 
hook. you can do this with clocks, but an explicit hook at 
the point where the gui is polled for updates would be much 
cleaner. maybe 2 hooks: one that runs at the dsp tick, and
one that runs at gui tick.

so the pattern is:
thread->pd:	a message queue with poller in pd scheduler
pd->thread:	another message queue with poller in worker thread

the pollers are application-specific, but some could be included
in the pd api, like a poller that sends messages to a certain object,
or a receiving symbol. this is not really necessary though.

my apologies if i missed out one some discussion on this subject,
but i think this would be trivial to add and very useful.

tom




More information about the Pd-dev mailing list