我正在审查关于“信号量小书”中可重复使用障碍的讨论"。在检查书中的答案之前,我写下了这个解决方案(下面)。由于书中的解决方案有点复杂(两个十字转门),我猜我错过了一些明显的东西。有人可以帮我指出任何问题吗?
# count = 0;
# mutex = semaphore(1);
# barrier = semaphore(0);
# rendezvous point
mutex.wait();
count = count + 1;
if (count == n) barrier.signal();
mutex.signal();
barrier.wait();
mutex.wait();
count = count - 1;
if (count != 0) barrier.signal();
mutex.signal();
# critical point
作为参考,这里使用两个十字转门(barrier1和barrier2)提出的解决方案
# count = 0;
# mutex = semaphore(1);
# barrier1 = semaphore(0);
# barrier2 = semaphore(1);
# rendezvous point
mutex.wait();
count = count + 1;
if (count == n)
barrier2.wait();
barrier1.signal();
mutex.signal();
barrier1.wait();
barrier1.signal();
# critical point
mutex.wait();
count = count – 1;
if (count == 0)
barrier1.wait();
barrier2.signal();
mutex.signal();
barrier2.wait();
barrier2.signal();