introduction
This is the fourteenth article on conditional variables for thread synchronization. In the previous article, we introduced read-write locks. When reading more and writing less, the performance of read-write locks is better than that of mutexes. This chapter also introduces an important method to handle thread synchronization -- condition variables
Conditional variable
- Conditional variable is a relatively complex method of thread synchronization
- Condition variables allow threads to sleep until certain conditions are met
- When the conditions are met, a signal can be sent to the thread to notify the wake-up thread
As for the producer consumer model introduced earlier, there are certain loopholes
- When the buffer is less than 0, consumers are not allowed to consume and must wait
- When the buffer is full, the producer is not allowed to produce in the buffer, and the producer must wait
In the previous article, there is no constraint on this. When this article studies condition variables, it will be more strictly constrained
Assuming that the buffer is equal to 0 at this time, when the producer produces a product, it wakes up the consumers who may be waiting. Assuming that the buffer is full, when a consumer consumes a product, it wakes up the producers who may be waiting
Examples of conditional variables (used in conjunction with mutexes)
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<pthread.h> #include<vector> #include<queue> //Critical resources int num=0; //Buffer Max int MAX_BUF=100; //Define condition variables pthread_cond_t cond=PTHREAD_COND_INITIALIZER; //Define mutex pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER; void* producer(void*){ while(true){ pthread_mutex_lock(&mutex); while(num>=MAX_BUF){ //wait for pthread_cond_wait(&cond, &mutex); printf("The buffer is full, waiting for consumers to consume\n"); } num+=1; printf("Produce a product. The current product quantity is:%d", num); sleep(1);//Time required to produce the product //Notify consumers who may be waiting pthread_cond_signal(&cond); printf("Notify consumer...\n"); //Unlock pthread_mutex_unlock(&mutex); sleep(1);Frequency of production } } void* consumer(void*){ while(true){ pthread_mutex_lock(&mutex); while(num<=0){ //wait for pthread_cond_wait(&cond, &mutex); printf("The buffer is empty, waiting for the producer to produce\n"); } num-=1; printf("Consume a product. The current product quantity is:%d", num); sleep(1); //Notify potential waiting producers pthread_cond_signal(&cond); printf("Notify producer...\n"); //Unlock pthread_mutex_unlock(&mutex); } } int main() { //Define two threads pthread_t thread1,thread2; //One executes producer logic and the other executes consumer logic pthread_create(&thread1, NULL, &producer, NULL); pthread_create(&thread2, NULL, &consumer, NULL); pthread_join(&thread1, NULL); pthread_join(&thread2, NULL); return 0; }
Execution result:
It is the core competitiveness of a technologist to find the unchangeable in the rapidly changing technology. Unity of knowledge and practice, combination of theory and Practice