Pthread_cond_wait () Understanding

Source: Internet
Author: User

Condition variable
Conditional variables are another synchronization mechanism available for threads. The conditional scalar provides a place for multiple threads to join. The conditional variables are used together with mutex, threads are allowed to wait for specific conditions in a non-competitive manner. The condition variable itself requires mutex protection. The thread must first lock the mutex before changing the condition State. Other threads will not notice this change before obtaining the mutex, because the mutex must be locked to calculate the condition!
The mutex parameter passed in by the opthread_cond_wait (pthread_cond_t * cond, pthread_mutex_t * mutex) function is used to protect the condition, because when we call pthread_cond_wait, if the condition is not true, we will be blocked, however, during the blocking period, if the condition variable changes, we will miss this condition. Because this thread has not been placed in the waiting queue, we need to lock the mutex before calling pthread_cond_wait, that is, call pthread_mutex_lock (). After pthread_cond_wait puts the thread into the blocking queue, automatically unlock the mutex so that other threads can obtain the lock right. In this way, other threads can access critical resources and wake up the blocked process as appropriate. When pthread_cond_wait returns, it automatically locks mutex.
The o function is used as follows:
/*********** Pthread_cond_wait **********/
Pthread_mutex_lock (& qlock);/* lock */
Pthread_cond_wait (& qready, & qlock);/* block --> unlock --> wait () return --> lock */
Pthread_mutex_unlock (& qlock);/* unlock */
/*************************************** **************/
 
The following is an example.

# Include <pthread. h> # include <stdio. h> # include <stdlib. h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/* initialize mutex lock */pthread_cond_t cond = PTHREAD_COND_INITIALIZER;/* initialize condition variable */void * thread1 (void *); void * thread2 (void *); int I = 1; int main (void) {pthread_t t_a; pthread_t t_ B; pthread_create (& t_a, NULL, thread1, (void *) NULL);/* create process t_a */pthread_create (& t_ B, NULL, thread2, (void *) NULL);/* create process t_ B */ Pthread_join (t_a, NULL);/* Wait for process t_a to end */pthread_join (t_ B, NULL);/* Wait for process t_ B to end */pthread_mutex_destroy (& mutex ); pthread_cond_destroy (& cond); exit (0);} void * thread1 (void * junk) {for (I = 1; I <= 6; I ++) {pthread_mutex_lock (& mutex);/* lock mutex */printf ("thread1: lock % d/n", _ LINE __); if (I % 3 = 0) {printf ("thread1: signal 1% d/n", _ LINE _); pthread_cond_signal (& cond);/* condition change, send a signal to notify the t_ B process */printf ("thread1: signal 2% D/n ", _ LINE _); sleep (1);} pthread_mutex_unlock (& mutex);/* unlock mutex */printf (" thread1: unlock % d/n ", _ LINE _); sleep (1) ;}} void * thread2 (void * junk) {while (I <6) {pthread_mutex_lock (& mutex); printf ("thread2: lock % d/n", _ LINE _); if (I % 3! = 0) {printf ("thread2: wait 1% d/n", _ LINE _); pthread_cond_wait (& cond, & mutex);/* unlock mutex, and wait until cond changes */printf ("thread2: wait 2% d/n", _ LINE _);} pthread_mutex_unlock (& mutex); printf ("thread2: unlock % d/n ", _ LINE _); sleep (1 );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.