static bool wants_in[2] static int turn
void peterson_in(int me) const int other = me == 1 ? 0 : 1 wants_in[me] = true turn = other while (wants_in[other] && (turn == other)) yield() // caller now has the stone.
void peterson_out(int me) // Caller releases the stone. wants_in[me] = false
static int in_critical_region void proc(int myid) for (int i = 0; i < 1000; i++) peterson_in(myid) assert(in_critical_region == 0) in_critical_region++ yield() assert(in_critical_region == 1) in_critical_region-- peterson_out(myid)
int main() wants_in[0] = wants_in[1] = false in_critical_region = 0 thread_create(proc, 0) thread_create(proc, 1)
This page last modified on 25 June 2002.