#include <semaphore.h>
sem_t sem;
Sem_init (&sem, 0, 0);
Sem_post (&sem);
Sem_wait (&sem);
Sem_destroy (&sem);
The data type of the semaphore is the structure sem_t, which is essentially a number of long integers. The function Sem_init () is used to initialize a semaphore. Its prototype is:
extern int Sem_init __p ((sem_t *__sem, int __pshared, unsigned int __value));
SEM is a pointer to the structure of the semaphore;
Pshared not 0 O'Clock this semaphore is shared among processes, otherwise it can be shared only for all threads of the current process;
Value gives the initial value of the semaphore.
The function sem_post (sem_t *sem) is used to increase the value of the semaphore.
When a thread is blocked on this semaphore, calling this function causes one of the threads to not block, and the selection mechanism is also determined by the thread's scheduling policy.
The function sem_wait (sem_t *sem) is used to block the current thread until the value of the semaphore SEM is greater than 0, and the value of the SEM is reduced by one after unblocking, indicating that the public resources are decreased after use. The function sem_trywait (sem_t *sem) is a non-blocking version of the function sem_wait (), which directly minimizes the value of the semaphore SEM.
The function Sem_destroy (sem_t *sem) is used to release the signal volume of the SEM.
---------------------------------------------------------------
The instances that have been seen are used on the main thread of blocking, and Sem_post (SEM) is used when the program runs the end resource release. To end the blocking.
Ext.: http://blog.sina.com.cn/s/blog_af9acfc6010191bj.html
The use of the signal volume SEM