// Consulting cap-lore.com/MathPhys/fft/MPfft/ft.c // Eight threads contend (with __sync_fetch_and_add) for indexes // within array t at which to store their thread number. #include #include #include #define cpus 8 typedef void * thnk(void *); static pthread_t mt(void * msg, thnk rtn){ pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); { pthread_t zz; int rc = pthread_create(&zz, &attr, rtn, msg); if(rc) {printf( "ERROR; return code from pthread_create() is %d\n", rc); exit(-1);} pthread_attr_destroy(&attr); return zz;}} typedef struct{int first; int * ret;} blkmsg; // unneeded type, but usually a way to send binary messages to each thread. #define sz 10000 int cur = 0; char t[sz]; int cnts[cpus]; void* fab(void* w){int me=((blkmsg*)w)->first; int p=0; while(1){int k = __sync_fetch_and_add(&cur, 1); if(k>=sz) break; t[k] = me+1; ++p;} printf("Bye %d %d\n", me+1, p); cnts[me] = p; return w;} int main(){for(int j=0; j