First, the condition variable
In the process of thread synchronization, it is necessary to wait for a certain condition to continue to execute, if the condition is not established, thread A is blocked, and thread B causes the condition to be set up during execution, wake thread A to continue execution. wait for a condition with conditional variable blocking in the Pthread library , or wake up the thread waiting for the condition. The condition variable is represented by a variable of type pthread_cond_t.
Initialize the condition variable with pthread_cond_init, if the condition variable is statically assigned, you can also use the macro to define Pthead_cond_initializer initialization, with Pthread_cond_de Stroy destroys the condition variable, returns 0 successfully, and returns the error number.
A condition variable is always used in conjunction with a mutex. A thread can call pthread_cond_wait to block the wait on a condition variable, which does the following three steps:
1. Releasing the mutex
2. Blocking wait
3. When awakened, regain the mutex and return
A thread can call pthread_cond_signal to wake up another thread waiting on a condition variable, or call Pthread_cond_broadcast to wake up all the threads waiting on the condition variable.
Ii. using producer-consumer models to illustrate
As the name implies, it can be seen that to achieve this model, first of all, there are two characters (producers, consumers), with two roles, of course, there must be an occasion for two access to the critical resources (an occasion), but also to understand the relationship between producers and manufacturers (mutual exclusion), consumer and consumer relations (mutual exclusion) , the relationship between producers and consumers (synchronization and mutual exclusion) is, in general, a place, with two roles and three relationships. by code, producers produce a data and then signal that consumers consume, consumers consume, and send signals to producers to tell producers to keep producing, so repeat.
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <malloc.h> 4 #include <pthread.h> 5 #include <semaphore.h> 6 typedef int Data_type; 7 typedef int* Data_type_p; 8 static pthread_mutex_t lock=pthread_mutex_initializer;//Initialize the mutex lock 9 static pthread_cond_t needproduct=pthread_cond_initializer;//Initialize condition variable 10 11 12 typedef struct listnode //define a linked list to hold data (a place) 13 { 14 Data_type data; 15 struct listnode* next; 16 }list ,*listp,** listpp; 17 18 listp head=null; 19 20 static listp Buynode (Data_type _data) 21 { 22 listp tem= (LISTP) malloc (sizeof (list)); 23 if (TEM) 24 { 25 tem -> data=_data; 26 &nBsp; tem -> next=null; 27 return tem; 28 } 29 return NULL; 30 } 31 void initlist (listpp list) 32 { 33 *list=buynode (0); 34 } 35 void push_list (Listp list,data_type _data) 36 { 37 listp cur=buynode (_data); 38 listp tem=list; 39 while (Tem->next) 40 { 41 tem=tem->next; 42 } 43 tem ->next=cur; 44 } 45 void deletelist (listp list) 46 { 47 if ( List) 48 &nbsP; { 49 free (list); 50 list=null; 51 } 52 } 53 int pop_list (Listp list,data_type_p data) 54 { 55 if (list ->next==null) 56 { 57 *data =-1; 58 return -1; 59 } 60 listp tem=list->next; 61 list ->next=tem->next; 62 *data=tem->data; 63 deletelist (TEM); 64 return 0; 65 } 66 void printlist (Listp list) 67 { 68 listp cur=list->next;; 69 while (cur) 70 { 71 printf ("%d", Cur->data); 72 fflush (stdout); 73 cur=cur- >next; 74 } 75 printf ("\ n"); 76 } 77 void *product (VOID*&NBSP;ARG)//define the relationship between producer and manufacturer (mutual exclusion) 78 { 79 int i=0; 80 while (1) 81 { 82 pthread_mutex_lock (&lock); 83 printf ("product data:%d\n", i); 84 push_list (head,i++); 85 &Nbsp; pthread_mutex_unlock (&lock); 86 printf ("conduct is ok.weak up comsumer...\n"); 87 pthread_cond_signal (&needproduct);//When the producer has data, send a signal to wake the consumer 88 sleep (2); 89 } 90 91 } 92 void *consumer (VOID*&NBSP;ARG)//consumer-to-consumer relationship (mutual exclusion) 93 { 94 data_type _data; 95 while (1) 96 { 97 pthread_mutex_lock (&lock); 98 while ( -1== Pop_list (head,&_data)) 99 {100 &nbSp; pthread_cond_wait (&needproduct,&lock);//wait until the news of the producer is confiscated 101 }102 printf ("consumer data:%d\n", _data); 103 pthread_mutex _unlock (&lock); 104 sleep (1);105 }106 }107 int main () 108 {109 initlist (&head); 110 pthread_t id1;111 pthread_t id2;112 pthread_create (&id1,null,product,null);113 Pthread_create (&id2,null,consumer,null); 114 pthread_join (Id1,NULL);115 pthread_join (id2,null); 116 return 0;117 }
Summary: The above code to achieve the single-producer and single-consumer, producer-consumer model, the simple is to achieve mutual exclusion between producers and manufacturers, consumers and consumers mutually exclusive, the relationship between producers and consumers synchronization mutually exclusive.
Linux--condition Variable (condition variable) implements producer-consumer model