对于详细的实现,我想知道互斥量和信号量之间的深层设计差异。我的问题是:
这是一些伪代码:
pthread_mutex_lock:
atomic_dec(pthread_mutex_t.value);
if(pthread_mutex_t.value!=0)
futex(WAIT)
else
success
pthread_mutex_unlock:
atomic_inc(pthread_mutex_t.value);
if(pthread_mutex_t.value!=1)
futex(WAKEUP)
else
success
sem_wait(sem_t *sem):
for (;;) {
if (atomic_decrement_if_positive(sem->count))
break;
futex_wait(&sem->count, 0)
sem_post(sem_t *sem)
n = atomic_increment(sem->count);
// Pass the new value of sem->count
futex_wake(&sem->count, n + 1);
}
sem_post(sem_t *sem):
n = atomic_increment(sem->count);
futex_wake(&sem->count, n + 1);