// declaration & initial values of global variables Enter, Number: array [1..N] of integer = {0};
// logic used by each thread... // where "(a, b) < (c, d)" // means "(a < c) or ((a == c) and (b < d))" Thread(i) { while (true) { Enter [i] = 1; Number[i] = 1 + max(Number[1],...,Number[N]); Enter [i] = 0; for (j=1; j<=N; ++j) { while (Enter[j] != 0) { // wait until thread j receives its number } while ((Number[j]!=0) && ((Number[j],j) < (Number[i],i))) { // wait until threads with smaller numbers // or with the same number, but with higher // priority, finish their work } } // critical section... Number[i] = 0; // non-critical section... } } 资料整理:IT技术精粹