Condition variables in Linux

Source: Internet
Author: User

1 You may know that a mutex is a necessary tool in a threaded program, but it cannot be omnipotent, such as when a thread is waiting for a condition of shared data to occur. It is possible to repeatedly attempt to lock and unlock the mutex to check the shared data structure.

The 2 thread puts the thread to sleep while waiting for certain conditions to be met, and then wakes up and waits for a thread to sleep if the condition satisfies the specific condition.

3 conditional variables generally allow a thread to block and wait for another thread to send a signal to compensate for the lack of a mutex.

4 Introduction to Functions

(1) static mode using pthread_cond_t Cond=pthread_cond_initializer

Dynamic mode int pthread_cond_init (pthread_cond_t *cond,pthread_condattr_t *cond_attr)

--------> Use the default property when Cond_attr is null.

(2) Unregister a condition variable

int Pthread_cond_destroy (pthread_cond_t *cond) can unregister this condition variable only if no thread is in the condition variable.

(3) Wait there are two ways, conditional wait and time wait. Either wait must mate with a mutex to prevent multiple threads from simultaneously requesting pthread_cond_wait () for this race condition.

(4) Excitation two ways, pthread_cond_signal () activates a thread waiting for the condition. Pthread_cond_broadcast () Activates all waiting threads.

5 examples

(1) A first example

1 #include <iostream>2 #include <pthread.h>3 #include <unistd.h>4 using namespace std;5pthread_cond_t qready = Pthread_cond_initializer; /* Initial CONSTRUCTION condition variable */6pthread_mutex_t qlock = Pthread_mutex_initializer; /* Initial Construction Lock */7int x = 10;8int y = 20;9void *func1 (void *Arg) {Tencout<<"func1 Start"<<Endl; OnePthread_mutex_lock (&qlock); A      while(x<y) -     { -Pthread_cond_wait (&qready,&qlock); the     } -Pthread_mutex_unlock (&qlock); -Sleep (3); -cout<<"Func1 End"<<Endl; + } -void *func2 (void *Arg) { +cout<<"Func2 Start"<<Endl; APthread_mutex_lock (&qlock); at//Modify XY -x = 20; -y = 10; -cout<<"Have change x and y"<<Endl; -Pthread_mutex_unlock (&qlock); -     if(X >y) { inPthread_cond_signal (&qready);//send a signal so that thread 1 does not block -     } tocout<<"Func2 End"<<Endl; + } -int main (int Argc,char * *argv) { the pthread_t Tid1,tid2; * int iRet; $IRet = Pthread_create (&tid1,null,func1,null);Panax Notoginseng     if(iRet) { -cout<<"pthread 1 Create error"<<Endl; the         returnIRet; +     } ASleep (2); theIRet = Pthread_create (&tid2,null,func2,null); +     if(iRet) { -cout<<"pthread 2 Create error"<<Endl; $         returnIRet; $     } -Sleep (5);  -     return0;  the}
View Code

(2) A second example

1 #include <stdio.h>2 #include <stdlib.h>3 #include <string.h>4 #include <unistd.h>5 #include <pthread.h>6 #include <errno.h>7 #include <iostream>8 #include <pthread.h>9 using namespace std;Ten  One/* Prompt for Taxi arrival condition variable */ Apthread_cond_t Taxicond =Pthread_cond_initializer; -/* Sync Lock */ -pthread_mutex_t Taximutex =Pthread_mutex_initializer;  the  -int travelercound=0; -//when the passengers arrive, the number adds 1. -void * Traveler_arrive (void *name) { +cout<<"Traveler:"<< (char *) name<<"needs a taxi now!"<<Endl; -Pthread_mutex_lock (&Taximutex); +travelercound++; APthread_cond_wait (&taxiCond,&Taximutex); atPthread_mutex_unlock (&Taximutex); -cout<<"Traveler:"<< (char *) name<<"Now got a taxi!"<<Endl; -Pthread_exit ((void*) 0); - } -  -void * Taxi_arrive (void *name) { incout<<"Taxi:"<< (char *) name<<"arrives."<<Endl; -//guaranteed first-come detection is there a new customer arrives to      while(1){ +Pthread_mutex_lock (&Taximutex); -//greater than the notification the         if(travelercound>0) { *Pthread_cond_signal (&taxicond); $Pthread_mutex_unlock (&Taximutex); Panax Notoginseng              Break;  -         } thePthread_mutex_unlock (&Taximutex); +     } APthread_exit ((void*) 0); the } +  - int main () { $pthread_t tids[3]; $int iRet = Pthread_create (&tids[0],null,taxi_arrive, (void*) ("Jack")); -     if(iRet) { -printf"pthread_create error:iret=%d\n", IRet); the         returnIRet; -     }Wuyiprintf"Time passing by.\n"); theSleep (1); -IRet = Pthread_create (&tids[1],null,traveler_arrive, (void*) ("Susan")); Wu     if(iRet) { -printf"pthread_create error:iret=%d\n", IRet); About         returnIRet; $     } -printf"Time passing by.\n"); -Sleep (1);  -IRet = Pthread_create (&tids[2],null,taxi_arrive, (void*) ("Mike")); A     if(iRet) { +printf"pthread_create error:iret=%d\n", IRet); the         returnIRet; -     }     $printf"Time passing by.\n"); theSleep (1); the      thevoid *retval;  the      for(int i=0;i<3;i++){ -Iret=pthread_join (tids[i],&retval); in         if(iRet) { theprintf"Pthread_join error:iret=%d\n", IRet); the             returnIRet; About         } theprintf"retval=%ld\n", (long) retval);  the     } the     return0;  +}

Good Dalat refueling!!!!

Condition variables in Linux

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.