1. Symptoms:
Brief description: A integer value for process transmission signals. It is expressed in Linux C ++ as SEM_T is included in <semaphore.h>.
Operation:
function SEM_INIT prototype is as follows:
/* Initialize semaphore object SEM to VALUE. If PSHARED then share it
with other processes. */
extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value)
__THROW;
parameter introduction:
__sem is a pointer for the initial SEM_T
__pshared is a shared attribute. It is not 0 when it is 0, otherwise it will only be shared between this process.
__value gives the initial value of the semaphore
function SEM_POST prototype is as follows:
/* Post SEM. */
extern int sem_post (sem_t *__sem) __THROWNL;
The role is to add the value of the transmitted signal to 1. When the thread is blocked by the signal volume, the thread can be released from the queue.
function SEM_WAIT prototype is as follows:
/* Wait for SEM being posted.
This function is a cancellation point and therefore not marked with
__THROW. */
extern int sem_wait (sem_t *__sem);
The role is to minimize the value of the transmitted signal. When the value of the signal is negative, the thread is blocked and placed in the queue until the lock is released.
Function SEM_DESTORY prototype is as follows:
/* Free resources associated with semaphore object SEM. */
extern int sem_destroy (sem_t *__sem) __THROW;
The function is to release the signal volume.
2. Condition semaphore
Brief description: Condition variables are actually similar to the semaphore, but only when the condition is true. It is expressed in Linux C ++ as pthread_cond_t, which is included in <pthread>.
Operation:
function pthread_cond_init prototypes are as follows:
/* Initialize condition variable COND using attributes ATTR, or use
the default values if later is NULL. */
extern int pthread_cond_init (pthread_cond_t *__restrict __cond,
const pthread_condattr_t *__restrict __cond_attr)
__THROW __nonnull ((1));
parameter introduction:
__cond requires the initial conditional signal pointer.
__cond_attr conditional variables.
Like mutual locks, the conditional semaphore can be set to share in the same process, or sharing between different processes, the defaultpthread_ process_private. That is, only the same process is shared.
Function pthread_wait function prototype is as follows:
/* Wait for condition variable COND to be signaled or broadcast.
MUTEX is assumed to be locked before.
This function is a cancellation point and therefore not marked with
__THROW. */
extern int pthread_cond_wait (pthread_cond_t *__restrict __cond,
pthread_mutex_t *__restrict __mutex)
__nonnull ((1, 2));
Parameter introduction:
__cond is a pointer of a certain condition semaphore.
__mutex adds a lock before.
The function of this function is that if the conditional semaphore does not meet the conditions, it will be blocked. Note that when this function enters, it will be unbuttoned and then wait for the changes in the condition variable. When the function returns, the lock will be locked before the function returns. freed. In fact, I found that this thing is no different from the semaphore, maybe I don’t know the essence of it.
Function pthread_cond_signal function prototype is as follows:
The function of
/* Wake up one thread waiting for condition variable COND. */
extern int pthread_cond_signal (pthread_cond_t *__cond)
__THROWNL __nonnull ((1));
This function is to add the value of the Cond to 1, which is the same as the signal amount.