Multi-thread----Condition Variables

Source: Internet
Author: User

Wait conditions are available in two ways: unconditional wait pthread_cond_wait () and timed wait pthread_cond_timedwait ()

There are two types of excitation conditions: pthread_cond_signal () activates a thread waiting for the condition, one is activated in the queued order when there are multiple waiting threads, and pthread_cond_broadcast () activates all waiting threads

Before calling Pthread_cond_wait (), the mutex must be Cheng (Pthread_mutex_lock ()), and before the update condition waits for the queue, the mutexes remain locked, and the thread hangs into the wait before unlocking.

The mutex will be re-locked to correspond to the lock action before entering Pthread_cond_wait () before the condition satisfies thereby leaving pthread_cond_wait ()

Lock the condition with a mutex to prevent the same condition from being contested by multiple threads.

The following example creates three threads, sets the global variable I, wakes thread 1 when i==1, wakes thread 2 when i==2, and wakes up thread 3 when i==3.

#include <stdio.h>#include <unistd.h>#include <pthread.h>int i =-1;p thread_mutex_t Mutex =pthread_mutex_initializer;pthread_cond_t condition =Pthread_cond_initializer;voidThread1 () {printf ("Therad1 begin\n"); Pthread_mutex_lock (&Mutex);while (i! =1) {printf ("Therad1 waiting\n"); Pthread_cond_wait (&condition, &Mutex); } printf ("Thread1 doing\n"); Pthread_mutex_unlock (&Mutex); printf"Thread1 end\n");}voidThread2 () {printf ("Therad2 begin\n"); Pthread_mutex_lock (&Mutex);while (i! =2) {printf ("Therad2 waiting\n"); Pthread_cond_wait (&condition, &Mutex); } printf ("Thread2 doing\n"); Pthread_mutex_unlock (&Mutex); printf"Thread2 end\n");}voidThread3 () {printf ("Therad3 begin\n"); Pthread_mutex_lock (&Mutex);while (i! =3) {printf ("Therad3 waiting\n"); Pthread_cond_wait (&condition, &Mutex); } printf ("Thread3 doing\n"); Pthread_mutex_unlock (&Mutex); printf"Thread3 end\n");}int main (int argc,Constchar *Argv[]) {pthread_t tid1; pthread_t tid2; pthread_t tid3;void *t1 =Thread1;void *t2 =Thread2;void *t3 = thread3; pthread_create (&tid1, NULL, T1, NULL); Pthread_create (&tid2, NULL, t2, NULL); Pthread_create (&tid3, NULL, T3, NULL); While (i! = 3) {i++; printf ("i =%d\n", i); Sleep (3); Pthread_cond_broadcast (&condition);} return 0;}                 

Output Result:

0123Thread3 doingThread3 End   

Summary: When the condition is not met, the thread hangs and waits for the condition to be met.

Because of the pthread_cond_broadcast used, the while loop is used to determine if the condition is true.

When the condition is not locked with a mutex:

0123
or/////////////.
i = 0
Therad1 Begin
Therad3 Begin
Therad2 Begin
Therad1 Waiting
Therad3 Waiting
Therad2 Waiting
i = 1
Thread1 doing
Thread1 End
i = 2
i = 3

Multi-thread----Condition Variables

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.