#ifndef BARRIER_H_ #define BARRIER_H_ #include // has pthread_ routines // ---- Class for barrier -------------------------------------------- class barrier { public: // Constructor: n is the number of threads that will participate. barrier(const int n) { nThreads = n; count = 0; pthread_mutex_init(&barrierLock, NULL); pthread_cond_init(&mustWait, NULL); } // Destructor. ~barrier(void) { pthread_mutex_destroy(&barrierLock); pthread_cond_destroy(&mustWait); } // Function all threads should call "at the barrier". void wait(void) { // Get lock on shared variables. pthread_mutex_lock(&barrierLock); if (++count < nThreads) { // not all threads have arrived yet -- wait pthread_cond_wait(&mustWait, &barrierLock); } else { // all threads have arrived -- notify waiting threads // and reset for next time pthread_cond_broadcast(&mustWait); count = 0; } pthread_mutex_unlock(&barrierLock); } private: // Make copy constructor and assignment operator private // so they can't be used (since it's not clear this would // make sense). barrier(const barrier & bb); barrier & operator= (const barrier & bb); // member variables int nThreads; // number of threads participating int count; // number that have arrived since last // reset pthread_mutex_t barrierLock; pthread_cond_t mustWait; }; #endif // BARRIER_H_