[PD] Externals with threads ...

seth at street-vision.com seth at street-vision.com
Tue Jan 25 02:17:02 CET 2005


/*****
Hello Pd mailist, 

I'm wondering what peoples opinions are on the subject of externals with their own threads. Are there reasons this is is not advised (e.g. reentrancy of subsequent calls)? To be specific I'm talking about threads that initiate output. I've looked at the sourcecode for netreceive and noticed the use of sys_addpollfn, but as this method is not included in m_pd.h, i wonder if it's even intended to be used in externals. One of the reasons i ask is that the example/test code attached bellow seems to work on one of my machines (Pentium 4/Fedora 2/PD 0.37.1), but when I run it on another (a PowerbookG4/Gentoo/PD 37.4) I get a stack overflow at the outlet_float. 

And if i'm not supposed to use threads, any insight on the use of these sys_*poll funtions. 

Regards,
-S

Code sample: thread_test.pd_linux
*****/

#include <m_pd.h>
#include <stdlib.h>
#include <pthread.h>

static t_class *thread_test_class;                                                                                    
typedef struct _thread_test {
  t_object x_obj;
  t_outlet *outlet;
  pthread_t tid;
} t_thread_test;
    
void *thread_test_new(void){
  t_thread_test *x = (t_thread_test *)pd_new(thread_test_class);
  x->outlet = outlet_new(&x->x_obj, &s_float);
  return x;
}
                                                                                static void *aThread(void *v){
  t_thread_test *x = (t_thread_test *)v;
  if (pthread_detach(pthread_self()) != 0){
    post("pthread_detach failed");
    return (NULL);
  }
  post("a thread is born");
  t_float aFloat = (t_float)12345;
  outlet_float(x->outlet, aFloat);
  return(NULL);
}
                                                                                void dotest(t_thread_test *x) {
  if (pthread_create(&x->tid, NULL, &aThread, (void *)x) != 0){
    post("Could not create the thread");
    return;
  }
}                                                                                    
void thread_test_setup(void){
  thread_test_class = class_new(gensym("thread_test"),
                       (t_newmethod)thread_test_new,
                       0,sizeof(t_thread_test),
                       CLASS_DEFAULT,0);
  class_addmethod(thread_test_class,(t_method)dotest,
                  gensym("test"),0);
}




More information about the Pd-list mailing list