struct sem_t
{
int value;
mutex_t mutex;
pthread_cond_t condt;
};
void sem_int(sem_t & t, int value)
{
t.value = value;
mutex_init(&mutex);
pthread_cond_init(&condt);
}
void sem_wait(sem_t & t)
{
mutex_lock(&t.mutex);
while(t.value <= 0)
{
pthread_cond_wait(&t.condt, &t.mutex);
}
--t.value;
mutex_unlock(&mutex);
}
void sem_post(sem_t & t)
{
mutex_lock(&t.mutex);
++t.value;
pthread_cond_signal(&t.condt);
mutex_unlock(&t.mutex);
}