How to create a fair multithreading double barrier?

133 views Asked by At

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) -

  1. mutex_lock;

  2. ++_barrier->m_predicate;

  3. /* 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); }

  4. 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);

0

There are 0 answers