I have a double barrier multi-thread program working, but I don't know how to create a fair mechanism (using POSIX mutex, conditional variable barrier functions) - meaning: groups of threads will enter the first barrier by arrival time to barrier.
Pseodo code for the code I have till now (summarized, original code has more validations. Hope it's clear enough) -
mutex_lock;
++_barrier->m_predicate;
/* block all threads ( except last at thread) - pending in barrier rendezvous point */
if(_barrier->m_predicate != _barrier->m_barrierSize) { pthread_cond_wait(&_barrier->m_cond, &_barrier->m_mutex); }
else { /* *Unblock all threads (by scheduling policy order) that are currently blocked by cond parameter in Barrier **Reset: Predicate value is "0" --> new batch of threads
enter 1st barrier */pthread_cond_broadcast (&_barrier->m_cond); ResetBarrier (_barrier); }
/* end of critical code block */
pthread_mutex_unlock(&_barrier->m_mutex);