sem_wait never returns a return value

205 views Asked by At

I am trying to create a lock using sem_wait() and I have a printf before and after the sem_wait call. (I do not see this being printed: printf("after sem wait ");) The sem_wait call never returns.

sem_t* sem;
Thank you
int 32_t retVal = -1;
printf("before sem wait ");
retVal  = sem_wait(sem);
printf("after sem wait ");

How do I debug this issue?

1

There are 1 answers

0
Dan Bonachea On

The argument to sem_wait must to point to an actual sem_t object which you've initialized using sem_init. For example:

sem_t sem;
sem_init(&sem, 1);
printf("before sem wait\n");
sem_wait(&sem);
printf("after sem wait\n");
sem_destroy(&sem);

Of course if you're using this object for actual signalling, you'll also need to arrange to call sem_post() elsewhere to increment the semaphore.