Linux Conditional Variable usage-general Linux technology-Linux programming and kernel information. The following is a detailed description. A condition variable is something in a thread, that is, waiting for a condition to happen, just like a signal.
The following describes how condition variables allow us to sleep and wait for certain conditions to appear.
A condition variable is a mechanism for synchronizing global variables shared by threads. It mainly includes two actions: one thread waits for the condition variable to be established and suspends; another thread sets "condition to true" (a signal indicating condition to true ). To prevent competition, the use of condition variables is always combined with a mutex lock.
The condition variable type is pthread_cond_t.
Create and log out
Like mutex lock, conditional variables have two static and dynamic creation methods. The static method uses the PTHREAD_COND_INITIALIZER constant, as shown below:
Pthread_cond_t cond = PTHREAD_COND_INITIALIZER
Call the pthread_cond_init () function dynamically. The API is defined as follows:
Int pthread_cond_init (pthread_cond_t * cond, pthread_condattr_t * cond_attr)
Although the POSIX standard defines attributes for condition variables, they are not implemented in LinuxThreads. Therefore, the cond_attr value is usually NULL and ignored.
To cancel a condition variable, you must call pthread_cond_destroy (). This condition variable can be canceled only when no thread is waiting for the condition variable. Otherwise, EBUSY is returned. The API is defined as follows:
Int pthread_cond_destroy (pthread_cond_t * cond)
Waiting and inspiring
Int pthread_cond_wait (pthread_cond_t * cond, pthread_mutex_t * mutex)
Int pthread_cond_timedwait (pthread_cond_t * cond, pthread_mutex_t * mutex, const struct timespec * abstime)
There are two waiting conditions: pthread_cond_wait () and pthread_cond_timedwait (). If the waiting time is not met before the given time, ETIMEOUT is returned and the waiting time ends, here, abstime appears in the absolute time format of the same meaning as the time () system call. 0 indicates GMT 00:00:00, January 1, January 1, 1970.
The advantage of using absolute time instead of relative time is that. If the function is returned in advance (probably because a signal is captured)
Either way, you must work with a mutex lock to prevent multiple threads from simultaneously requesting the Race Condition of pthread_cond_wait () (or pthread_cond_timedwait (), the same below ). Mutex locks must be common locks (PTHREAD_MUTEX_TIMED_NP) or adaptive locks (PTHREAD_MUTEX_ADAPTIVE_NP), and must be locked by this thread before pthread_cond_wait () is called ()), before the update condition is waiting for the queue, mutex remains locked and is unlocked before the thread is suspended and enters the waiting state. Before the condition is met and pthread_cond_wait () is left, mutex is re-locked to match the lock action before pthread_cond_wait.
There are two ways to activate the condition. pthread_cond_signal () activates a thread waiting for the condition. When there are multiple waiting threads, one of them is activated in the queue order; and pthread_cond_broadcast () activate all the waiting threads.
Others
Pthread_cond_wait () and pthread_cond_timedwait () are both implemented as cancellation points. Therefore, the thread waiting for this point will immediately run again and leave pthread_cond_wait () after re-locking mutex (), then, cancel the operation. That is to say, if pthread_cond_wait () is canceled, mutex remains locked. Therefore, you need to define the exit callback function to unlock it.
The following example demonstrates the combination of mutex lock and condition variable, and the impact of cancellation on the conditional wait action. In this example, two threads are started and wait for the same condition variable. If you do not use the exit callback function (see the comments section in this example), tid2 will be in pthread_mutex_lock () wait permanently. If the callback function is used, the condition wait of tid2 and the condition excitation of the main thread can work normally.
Example