IMPORT_C int | sem_init | ( | sem_t * | sem, |
int | pshared, | |||
unsigned int | value | |||
) |
IMPORT_C int | sem_destroy | ( | sem_t * | sem | ) |
The sem_destroy function destroys the unnamed semaphore pointed to by sem . After a successful call to sem_destroy , sem is unusable until re-initialized by another call to sem_init .
sem_t psem; if (sem_init(&psem;, 0, 1) < 0) { perror("sem_init"); return -1; } if (sem_destroy(&psem;) == 0) return -1; }
See also: sem_init()
Parameters | |
---|---|
sem | pointer to sem_t object that needs to be destroyed |
IMPORT_C int | sem_trywait | ( | sem_t * | sem | ) |
See also: sem_getvalue() sem_post()
Parameters | |
---|---|
sem | pointer to sem_t object on which to wait |
IMPORT_C int | sem_wait | ( | sem_t * | sem | ) |
The sem_wait function decrements (locks) the semaphore pointed to by sem , but blocks if the value of sem is zero, until the value is non-zero and the value can be decremented.
The sem_trywait function decrements (locks) the semaphore pointed to by sem only if the value is non-zero. Otherwise, the semaphore is not decremented and an error is returned.
sem_t psem; if (sem_init(&psem;, 0, 1) < 0) { perror("sem_init"); return -1; } /* Lock Semaphore */ if( sem_wait(psem) == -1 ) { perror("sem_wait failed"); return -1; } else printf ("Locked successfully ");
sem_t psem; if (sem_init(&psem;, 0, 1) < 0) { perror("sem_init"); return -1; } /* Lock Semaphore */ if ( sem_trywait(psem) == 0) { switch(errno) { case 0: printf ("Locked successfully"); break; case EAGAIN: printf ("could not lock, try later....."); break; } } else printf ("Sem_trywait failed");
See also: sem_getvalue() sem_post()
Parameters | |
---|---|
sem | pointer to sem_t object on which to wait |
IMPORT_C int | sem_timedwait | ( | sem_t * | sem, |
const struct timespec * | abstime | |||
) |
The sem_timedwait function decrements (locks) the semaphore pointed to by sem , but blocks if the value of sem is zero, until the value is non-zero or timeout occurs.
sem_t mysemp; struct timespec ts; int val, sts; #define TIMEOUT 3 if ( sem_init (&mysemp;, 0, 1) == -1 ) { perror( "sem_init failed"); return -1; } struct timeval now; gettimeofday(&now;,NULL); ts.tv_sec=now.tv_sec + TIMEOUT; ts.tv_nsec=0; /* Lock Semaphore */ if (sem_timedwait(&mysemp;, &ts;) == 0) { switch(errno) { case 0: printf ("Locked successfully"); break; case ETIMEDOUT: printf ("could not lock, try later....."); break; } } else printf ("Sem_timedwait failed ") fprintf(stderr,"Thread wakened up");
See also: sem_getvalue() sem_post()
Parameters | |
---|---|
sem | pointer to sem_t object on which to wait |
abstime | pointer to timespec struct specifying the absolute time till which to wait |
IMPORT_C int | sem_post | ( | sem_t * | sem | ) |
The sem_post function increments (unlocks) the semaphore pointed to by sem . If there are threads blocked on the semaphore when sem_post is called, then the highest priority thread that has been blocked the longest on the semaphore will be allowed to return from sem_wait .
sem_t psem; if (sem_init(&psem;, 0, 1) < 0) { perror("sem_init"); return -1; } if( sem_post(psem) == -1 ) { perror( "sem_post failed"); return -1; }
See also: sem_getvalue() sem_trywait() sem_wait()
Parameters | |
---|---|
sem | pointer to sem_t object which should be signalled |