弱读者优先
在这一版本的实现中需要在已经有读者在读的情况下优先响应正在等待的读者而不是写者,代码如下:
int rc = 0;
sem_t mutex, w;
void reader()
{
while(true) {
P(&mutex);
rc++;
if(rc == 1)
P(&w);
V(&mutex);
...read...
P(&mutex);
rc--;
if(rc == 0)
V(&w);
V(&mutex);
}
}
void writer()
{
while(true)
{
P(&w);
...write...
V(&w);
}
}
在这个方案中只有第一个读者需要和写者一起排队,而其他读者与第一个读者有相同的优先级,相当于站在第一个读者的旁边&#x