Pthread_cond_wait () before use whileexplanation the-Ten- - 9: -in a Linux environment, multithreaded programming is bound to encounter situations where conditional variables are required, and the pthread_cond_wait () function must be used at this time. But the execution of this function is more difficult to understand. The workflow for Pthread_cond_wait () is as follows (in the case of example in man): Consider, shared variables x and Y,protectedBy the mutex mut, and a condition vari-Able Cond that isTo is signaled whenever x becomes greater than Y. intx, y; pthread_mutex_t Mut=Pthread_mutex_initializer; pthread_cond_t Cond=Pthread_cond_initializer; Waiting until X isGreater than Y isPerformed asFollows:pthread_mutex_lock (&mut); while(x <=y) {pthread_cond_wait (&cond, &mut); } /*operate on X and Y*/Pthread_mutex_unlock (&mut); Modifications on x and y so cause X to become greater than y should signal the con-ditionifNeeded:pthread_mutex_lock (&mut); /*modify x and y*/ if(x > Y) pthread_cond_broadcast (&cond); Pthread_mutex_unlock (&mut); This example means that two threads are going to modify the values of x and Y, the first thread when x<=y is suspended until X>y is executed (the value of x, Y, which may be modified by the second thread, when the x>y wakes the first thread), that is, first initializes a common mutex mut and a condition variable cond. The following function bodies are then executed separately in two threads: Pthread_mutex_lock (&mut); while(x <=y) {pthread_cond_wait (&cond, &mut); } /*operate on X and Y*/Pthread_mutex_unlock (&mut); and: Pthread_mutex_lock (&mut); /*modify x and y*/ if(x > Y) pthread_cond_signal (&cond); Pthread_mutex_unlock (&mut); In fact, the execution of the function is very simple, the first thread executes to pthread_cond_wait (&cond,&mut), at this point if x<=Y, this function unlocks the Mut mutex and then locks the cond condition variable, at which point the first thread hangs (no CPU cycles are consumed). In the second thread, it was originally blocked because Mut was locked by the first thread, and because Mut has been released, the lock Mut can be obtained, and the values of x and Y are modified, and after modification, an if statement determines whether X>Y, if it is, then the pthread_cond_signal () function wakes the first thread and releases the mutex mut in the next sentence. Then the first thread starts to execute from Pthread_cond_wait (), first to lock the Mut again, if the lock succeeds, then the condition is judged (as to why it is used while, that is, after being awakened, there is a reason for analysis), and if the condition is met, it is awakened for processing. Finally release the mutex Mut. As for why the conditional judgment (i.e. why the while loop is used to judge the condition) after being awakened is because there may be a "surprise group effect". Some people think that since it is awakened, must be satisfied with the conditions, it is not. If multiple threads are waiting for this condition and only one thread can handle it at the same time, the condition must be judged again so that only one thread enters the critical section. To this, turn to a paragraph: reference under POSIX rationale:condition wait semantics It isImportant to note this when pthread_cond_wait () and pthread_cond_timedwait ()returnWithout error, the associated predicate may still befalse. Similarly, when pthread_cond_timedwait () returns with the timeout error, the associated predicate could betruedue to a unavoidable race between the expiration of the timeout and the predicate state. The application needs to recheck the predicate on anyreturnBecause it cannot be sure there isAnother thread waiting on the thread to handle the signal, andifThere isNot and then the signal isLost. The burden isOn the application to check the predicate. Some implementations, particularly on a multi-processor, may sometimes cause multiple threads to wake up when the condition variable issignaled simultaneously on different processors. In general, whenever a condition wait returns, the thread have to re-evaluate the predicate associated with the condition wait to determine whether it can safely proceed, should wait again, Or should declare a timeout. Areturn fromThe wait does not imply that the associated predicate isEithertrueOrfalse. It isThus recommended that a condition wait be enclosedinchThe equivalent of a"While Loop"that checks the predicate. As can be seen from the above:1, pthread_cond_signal may wake up multiple threads at the same time on multiple processors, and when you have only one thread to process a task, the other wake-up threads will need to continue wait,while the meaning of the loop is here, and the specification requires Pthread_ Cond_signal wakes up at least one thread on a pthread_cond_wait, but some implementations can also wake up multiple threads on a single processor in order to be simple. 2, some applications, such as the thread pool, Pthread_cond_broadcast wake up all threads, but we usually only need a subset of the threads to perform the task, so other threads need to continue wait. It is highly recommended to use the while loop here. In fact, it is simple to say that pthread_cond_signal () can also wake up multiple threads, and if you only allow one thread to access it, you must use while to make conditional judgments to ensure that only one thread in the critical section is processing.
Pthread_cond_wait () before using the while tutorial