我试图找出如何使用信号量实现监视器。我写的(伪)代码看起来像这样:
# var globales
sem e=1, unlocked=0, wait[]={0}
int nw=0, waitlist[]={0}
bool mLock = true
entry(monitor):
P(e)
if mLock:
nw++
V(e)
P(unlocked)
nw--
mLock = true
exit(monitor):
mLock = false
if (nw > 0): V(unlocked)
else: V(e)
wait(monitor, cv):
V(e)
waitlist[cv] += 1
P(wait[cv])
waitlist[cv] -= 1
P(e)
signal(monitor, cv):
if waitlist[cv] > 0: V(wait[cv])
此实施是否有效?