[LAD] question about multithreaded externals in Pd

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <pd-list@...>, <pd-dev@...>
Cc: <linux-audio-dev@...>
Date: Saturday, October 2, 2010 - 12:36 am

Hi all,

I am wondering if anyone can shed some light on the following
predicament. I am by no means a multi-threading guru so any insight
would be most appreciated.

The following are relevant excerpts from the code of an external. AFAIK
the external initializes mutex and cond and spawns a secondary worker
thread that deals with audio-unfriendly (xrun-causing) write operations
to the wiimote and terminates it when the object is destructed waiting
for the thread to join back and then destroying the mutex.

Now, if I add a bit of usleep right after the thread has been spawned as
part of the constructor (as included below) the external seems very
stable (e.g. cutting and pasting it as fast as keyboard allows, or in
other words constructing and destructing instances of it as fast as
possible does not result in a crash). Yet, when one does not use usleep
right after spawning the secondary (worker) thread in the constructor,
the whole thing is very crash-prone, almost as if the spawning of thread
does not go well unless given adequate time to do get things all into
sync, so to say, even though this makes to me no sense as the way I
understand it the constructor does not move ahead until pthread_create
does not return a value (which in this case I am not bothering to read).

Curiously, when not using usleep, a crash may occur right at creation
time, at any point while the object exists, and even as late as during
its destruction. Any ideas?

P.S. I am also including the entire file for those interested in trying
it out.

Best wishes,


Relevant excerpts (in random order and incomplete to allow for greater

//struct defining the object
typedef struct _wiimote
t_object x_obj; // standard pd object (must be first in struct)


//Creating separate threads for actions known to cause sample drop-outs
pthread_t unsafe_t;
pthread_mutex_t unsafe_mutex;
pthread_cond_t unsafe_cond;

t_float unsafe;


t_float led;


} t_wiimote;

static void *pd_cwiid_new(t_symbol* s, int argc, t_atom *argv)

x->led = 0;

// spawn threads for actions known to cause sample drop-outs
threadedFunctionParams rPars;
rPars.wiimote = x;
pthread_mutex_init(&x->unsafe_mutex, NULL);
pthread_cond_init(&x->unsafe_cond, NULL);
pthread_create( &x->unsafe_t, NULL, (void *)
&pd_cwiid_pthreadForAudioUnfriendlyOperations, (void *) &rPars);

//WHY IS THIS NECESSARY? I thought that pthread_create call will first
finish spawning thread before proceeding
usleep(100); //allow thread to sync (is there a better way to do this?)


static void pd_cwiid_free(t_wiimote* x)
if (x->connected) {
pd_cwiid_doDisconnect(x); //this one has nothing to do with thread but
rather disconnects the wiimote

x->unsafe = -1; //to allow secondary thread to exit the while loop


pthread_join(x->unsafe_t, NULL);


//worker thread
void pd_cwiid_pthreadForAudioUnfriendlyOperations(void *ptr)
threadedFunctionParams *rPars = (threadedFunctionParams*)ptr;
t_wiimote *x = rPars->wiimote;
t_float local_led = 0;
t_float local_rumble = 0;
unsigned char local_rpt_mode = x->rpt_mode;

while(x->unsafe > -1) {
if ((local_led == x->led) && (local_rumble == x->rumble) &&
(local_rpt_mode == x->rpt_mode)) {
pthread_cond_wait(&x->unsafe_cond, &x->unsafe_mutex);

if (local_led != x->led) {
local_led = x->led;
//do something
if (local_rumble != x->rumble) {
local_rumble = x->rumble;
//do something else



//an example of how the thread is affected by the main thread
void pd_cwiid_setLED(t_wiimote *x, t_floatarg f)
if (x->connected) {
x->led = f;


Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[LAD] question about multithreaded externals in Pd, Ivica Ico Bukvic, (Sat Oct 2, 12:36 am)
Re: [LAD] question about multithreaded externals in Pd, Robin Gareus, (Sat Oct 2, 1:01 am)
Re: [LAD] question about multithreaded externals in Pd, Ivica Ico Bukvic, (Sat Oct 2, 1:13 am)
Re: [LAD] question about multithreaded externals in Pd, Robin Gareus, (Sat Oct 2, 1:24 am)