信号量
什么是信号量
class Semaphore {
int sem;
WaitQueue q;
}
Semaphore::P(){
sem--;//消耗一个苹果
if(sem < 0){//不够了
Add this thread t to q;
block(t)
}
}
Semaphore::V(){
sem++;//添加一个苹果
if(sem <= 0){//发现有人在冬眠等苹果
Remove a thread t from q;
wakeup(t);
}
}+信号量的一些应用
临界区互斥访问
用信号量实现条件同步
Last updated