/* * Dekker's critical section algorithm, implemented with fences. * * URL: * http://www.justsoftwaresolutions.co.uk/threading/ */ #include #include #include "librace.h" std::atomic flag0, flag1; std::atomic turn; std::atomic var; void p0() { flag0.store(true); while (flag1.load()) { if (turn.load() !=0) { flag0.store(false); while (turn.load() != 0) { thrd_yield(); } flag0.store(true); } else thrd_yield(); } // critical section var.store(1); turn.store(1); flag0.store(false); } void p1() { flag1.store(true); while (flag0.load()) { if (turn.load() != 1) { flag1.store(false); while (turn.load() != 1) { thrd_yield(); } flag1.store(true); } else thrd_yield(); } // critical section var.store(2); turn.store(0); flag1.store(false); } void p0l(void *arg) { int i; for(i=0;i