Reputation: 21
To enter into if, I need to reach the value in "t" variable. Is there any way I can do this?
int main(int argc , char* argv[]){
sem_t t;
sem_init(&t, 0 /*#ofP*/, 1/*Semaphore start value*/);
if(t > 0){
printf("Hello");
}
return 0;
}
Upvotes: 1
Views: 1933
Reputation: 25388
You can use sem_getvalue (error checking omitted for brevity):
sem_t t;
sem_init (&t, 0 /*#ofP*/, 1 /*Semaphore start value*/);
int sval;
sem_getvalue (&t, &sval);
if (sval > 0)
printf ("Hello");
However: semaphores are generally used in a multi tasking / multi threaded context, so the value can change from underneath you at any time. If your goal is to wait on the semaphore until it is signalled, use sem_wait
(or sem_trywait
or sem_timedwait
) instead.
Upvotes: 3