1. Conditional Variable Initialization
[Cpp]
Int pthread_cond_init (thread_cond_t * cond, pthread_condattr_t * attr );
Int pthread_cond_init (thread_cond_t * cond, pthread_condattr_t * attr); parameter: cond condition variable
Attr condition variable attributes
0 is returned for success, and an error code is returned.
Note: If the attr parameter is null, it uses the default attribute to set the specified condition variable.
2. Condition variable destroy Function
[Cpp]
Int pthread_cond_destroy (pthread_cond_t * cond );
Int pthread_cond_destroy (pthread_cond_t * cond); 0 is returned for success, and error code is returned for error.
Note: If the specified condition variable is destroyed, the resources allocated to it will be released. The process that calls this function does not need to wait for the conditional variable specified by the parameter.
3. Conditional Variable wait Function
[Cpp]
Int pthread_cond_wait (pthread_cond_t * cond, pthread_mutex_t * mutex );
Int pthread_cond_timedwait (pthread_cond_t * cond, pthread_mutex_t * mytex, const struct timespec * abstime );
Int pthread_cond_wait (pthread_cond_t * cond, pthread_mutex_t * mutex );
Int pthread_cond_timedwait (pthread_cond_t * cond, pthread_mutex_t * mytex, const struct timespec * abstime); parameter: cond condition variable, mutex lock
Note: the type of the function pthread_cond_timedwait is different from that of the function pthread_cond_wait. The difference is that if the parameter * abstime is reached or exceeded, it ends and returns an error ETIME.
Typedef struct timespec
{
Time_t TV _sec; // second
Long TV _nsex; // millisecond
} Timespec_t;
That is to say, an error is returned when the time exceeds the preset value!
4. Conditional Variable notification function
[Cpp]
Int pthread_cond_signal (pthread_cond_t * cond );
Int pthread_cond_broadcast (pthread_cond_t * cond );
Int pthread_cond_signal (pthread_cond_t * cond );
Int pthread_cond_broadcast (pthread_cond_t * cond); parameter: cond condition variable
0 is returned for success, and an error code is returned.
Pthread_cond_signal: Only one thread is awakened.
Pthread_cond_broadcast: Wake up all threads.
Note:
When pthread_cond_signal is called, a thread that is blocked on the same condition variable will be unlocked. If multiple threads are blocked at the same time, the scheduling policy determines the thread that receives the notification. If pthread_cond_broadcast is called, all threads blocked on this condition variable are notified. Once awakened, the thread still requires mutex lock. If no thread is waiting for notification, the above two calls actually become a null operation. If the * cond parameter points to an Invalid Address, the return value is Val.
5. Initialization/destruction functions of condition variable attributes
[Cpp]
Pthread_condattr_t attr;
Int pthread_condattr_init (pthread_condattr_t * attr );
Int pthread_condattr_destroy (pthread_condattr_t * attr );
Pthread_condattr_t attr;
Int pthread_condattr_init (pthread_condattr_t * attr );
Int pthread_condattr_destroy (pthread_condattr_t * attr); Return Value: 0 if success is returned, and error number if failure is returned.
Once a condition variable object is initialized, we can use the following function to view or modify specific attributes.
6. View or modify the conditional variable attribute function
[Cpp]
Int pthread_condattr_getpshared (pthread_condattr_t * attr, int * pshared );
Int pthread_condattr_setpshared (pthread_condattr_t * attr, int pshared );
Int pthread_condattr_getpshared (pthread_condattr_t * attr, int * pshared );
Int pthread_condattr_setpshared (pthread_condattr_t * attr, int pshared); values of pshared:
PTHREAD_PROCESS_PRIVATE (default): The condition variable can be used by a thread in a process.
PTHREAD_PROCESS_SHARED: Condition variables can be used by threads in multiple processes.
Note: To use a PTHREAD_PROCESS_SHARED condition variable, you must use a PTHREAD_PROCESS_SHARED mutex, because the two threads that use a condition variable must use the same mutex.
[Cpp]
/*
* Cond_attr.c
*
* Main () creates a condition variable using a non-default attributes object,
* Cond_attr. If the implementation supports the pshared attribute,
* Condition variable is created "process private". (Note that, to create
* "Process shared" condition variable, the pthread_cond_t itself must be
* Placed in shared memory that is accessible to all threads using
* Condition variable .)
*/
# Include <pthread. h>
# Include "errors. h"
Pthread_cond_t cond;
Int main (int argc, char * argv [])
{
Pthread_condattr_t cond_attr;
Int status;
Status = pthread_condattr_init (& cond_attr );
If (status! = 0)
Err_abort (status, "Create attr ");
# Ifdef _ POSIX_THREAD_PROCESS_SHARED
Status = pthread_condattr_setpshared (
& Cond_attr, PTHREAD_PROCESS_PRIVATE );
If (status! = 0)
Err_abort (status, "Set pshared ");
# Endif
Status = pthread_cond_init (& cond, & cond_attr );
If (status! = 0)
Err_abort (status, "Init cond ");
Return 0;
}
/*
* Cond_attr.c
*
* Main () creates a condition variable using a non-default attributes object,
* Cond_attr. If the implementation supports the pshared attribute,
* Condition variable is created "process private". (Note that, to create
* "Process shared" condition variable, the pthread_cond_t itself must be
* Placed in shared memory that is accessible to all threads using
* Condition variable .)
*/
# Include <pthread. h>
# Include "errors. h"
Pthread_cond_t cond;
Int main (int argc, char * argv [])
{
Pthread_condattr_t cond_attr;
Int status;
Status = pthread_condattr_init (& cond_attr );
If (status! = 0)
Err_abort (status, "Create attr ");
# Ifdef _ POSIX_THREAD_PROCESS_SHARED
Status = pthread_condattr_setpshared (
& Cond_attr, PTHREAD_PROCESS_PRIVATE );
If (status! = 0)
Err_abort (status, "Set pshared ");
# Endif
Status = pthread_cond_init (& cond, & cond_attr );
If (status! = 0)
Err_abort (status, "Init cond ");
Return 0;
}