#define DEFAULT_NTHREADS 4 int nthreads = DEFAULT_NTHREADS; void barrier(int tid) { static volatile unsigned long count = 0; static volatile unsigned int sense = 0; static volatile unsigned int thread_sense[MAX_THREADS] = {0}; thread_sense[tid] = !thread_sense[tid]; if (fai(&count) == nthreads-1) { count = 0; sense = !sense; } else { while (sense != thread_sense[tid]); /* spin */ } }