Fundamentals of computer operating system (14) — conditional variables of thread synchronization

Time:2021-3-5

introduction

This paper is the fourteenth,Conditional variables for thread synchronizationIn the last article, I introduced the read-write lock. In the case of more reading and less writing, the performance of read-write lock is better than mutex. This article introduces an important method of processing thread synchronization—Conditional variable

Conditional variable

  • Conditional variable is a relatively complex thread synchronization method
  • Condition variables allow threads to sleep until a condition is met
  • When the conditions are met, a signal can be sent to the thread to notify the thread to wake up

There are some loopholes in the producer consumer model

  • When the buffer is less than 0, consumers are not allowed to consume and they have to wait
  • When the buffer is full, producers are not allowed to produce in the buffer. Producers must wait

In the previous article, there is no constraint on this. In this paper, when learning conditional variables, we will make a more rigorous constraint on this

Suppose that the buffer is equal to 0,When a producer produces a product, it wakes up the consumers who may be waiting. Suppose the buffer is full,When consumers consume a product, they wake up the producers who may wait

Examples of conditional variables (to be used with mutex)

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<vector>
#include<queue>

//Critical resources
int num=0;
//Maximum buffer size
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
            pthread_cond_wait(&cond, &mutex);
            Printf ("the buffer is full, waiting for the consumer to consume it");
        }
        num+=1;
        Printf ("to produce a product, the current product quantity is% d", Num) ";
        Sleep (1); // production time
        //Notify consumers who may be waiting
        pthread_cond_signal(&cond);
        Printf ("notify consumer... \ n");
        //Unlocking
        pthread_mutex_unlock(&mutex);
        Sleep (1); the frequency of production
    }
}

void* consumer(void*){
    while(true){
        pthread_mutex_lock(&mutex);
        while(num<=0){
            //Wait
            pthread_cond_wait(&cond, &mutex);
       Printf ("the buffer is empty, waiting for the producer to produce '";     
        }
        num-=1;
        Printf ("consume a product, the current product quantity is% d", Num) ";
        sleep(1);
        //Notify producers who may be waiting
        pthread_cond_signal(&cond);
        Printf ("notify producer... \ n");
        //Unlocking
        pthread_mutex_unlock(&mutex);
    }
}

int main()
{
        //Define two threads
        pthread_t thread1,thread2;
        //One is producer logic, the other is consumer logic
        pthread_create(&thread1, NULL, &producer, NULL);
        pthread_create(&thread2, NULL, &consumer, NULL);
        pthread_join(&thread1, NULL);
        pthread_join(&thread2, NULL);
        
        return 0;
}

Implementation results:
Fundamentals of computer operating system (14) -- conditional variables of thread synchronization

It is the core competitiveness of a technologist to seek invariability in the rapidly changing technology. The unity of knowledge and practice, the combination of theory and Practice

Fundamentals of computer operating system (14) -- conditional variables of thread synchronization