0

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

user8180105
  • 27
  • 1
  • 2
  • To clarify - code is written in C – user8180105 Oct 28 '17 at 13:15
  • [The explanation why fair locks are an incredibly bad idea](http://joeduffyblog.com/2006/12/14/anticonvoy-locks-in-windows-server-2003-sp1-and-windows-vista/). And that's a fundamental issue even for the operating system itself. Trying to reimplement them in user code will be even worse. – Voo Oct 28 '17 at 14:03
  • OK - fair multi thread barrier (double barrier) was the instruction. OS is linux, not windows (relevant functions are POSIX, not system V). – user8180105 Oct 28 '17 at 14:29
  • Well if you're doing homework you should be doing it on your own. If it's not homework you should consider the problem and change your approach to not require a fair lock - that's generally th not too difficult – Voo Oct 28 '17 at 14:48
  • Any other answer? – user8180105 Oct 29 '17 at 16:07

0 Answers0