Linux mutual exclusion and synchronization applications (iii): POSIX threading implements a single producer and a single consumer model

Source: Internet
Author: User

Copyright Notice: respect for the original. Reprint please retain source: blog.csdn.net/shallnet or .../gentleliu. The article is for academic communication only and should not be used for commercial purposes "In the first section, we talk about producer consumer issues, and this section lets us implement a slightly modified model: the initial buffer is empty. The producer writes data to the buffer. The consumer sleeps when the buffer is empty and notifies the consumer to start spending when the producer fills half the buffer. The producer starts to sleep. Until the consumer consumes half of the buffer and notifies the producer to start production, the access of producers and consumers to the buffer zone is mutually exclusive. Take a look at the implementation:
#include <stdio.h> #include <pthread.h> #include <unistd.h> #define Max_buffer 6int BUFFER = 0;pthread_ mutex_t Mutex = pthread_mutex_initializer;pthread_cond_t Cond_consumer = pthread_cond_initializer;pthread_cond_t cond _productor = pthread_cond_initializer;void *consumer_th (void *arg) {for (;;)        {Pthread_mutex_lock (&mutex); if (buffer <= 0) {printf ("=%s->%d=====consumer wait productor to product, start sleep ..., buffer=%d\n",            __func__, __line__, buffer); Pthread_cond_wait (&cond_consumer, &mutex);//When the consumer discovers that the buffer is empty, start sleep} buffer--;        Consumer consumes a buffer of printf ("=%s->%d====consumer consume a buffer, buffer=%d\n", __func__, __line__, buffer);            if (buffer < MAX_BUFFER/2) {pthread_cond_signal (&cond_productor);//Notify producers to start production when consumers find less than half of the buffer        printf ("=%s->%d====notify productor ..., buffer:%d\n", __func__, __line__, buffer);  } pthread_mutex_unlock (&mutex);      Sleep (3); } return NULL; void *productor_th (void *arg) {for (;;)        {sleep (1);        Pthread_mutex_lock (&mutex); if (buffer >= max_buffer) {printf ("=%s->%d=====productor wait consumer to consume, start sleep ..., Buffe            R=%d\n ", __func__, __line__, buffer); Pthread_cond_wait (&cond_productor, &mutex);//When the buffer is full. Pause Production} buffer++;//producer adds a buffer to printf ("=%s->%d====product Add a buffer ..., buffer:%d\n", __func__,        __line__, buffer); if (Buffer > MAX_BUFFER/2) {pthread_cond_signal (&cond_consumer);//When buffers are added to half, inform consumers to start consuming PRI        NTF ("=%s->%d====notify consumer ..., buffer:%d\n", __func__, __line__, buffer);    } pthread_mutex_unlock (&mutex); } return NULL;    int main (int argc, const char *argv[]) {pthread_t tid_productor, Tid_consumer; if (0! = pthread_create (&tid_consumer, NULL, consumer_th, NULL)) {printf ("pthread_create failed!\n");       return-1;        } if (0! = pthread_create (&tid_productor, NULL, productor_th, NULL)) {printf ("pthread_create failed!\n");    return-1;    } pthread_join (Tid_productor, NULL);    Pthread_join (Tid_consumer, NULL); return 0;}
The following are the program execution output results:
=consumer_th->20=====consumer wait productor to product, start sleep ..., buffer=0//the consumer starts sleeping waiting for the producer to wake up =productor_th- >54====product Add a buffer ..., buffer:1=productor_th->54====product add a buffer ..., buffer:2=productor_th-> 54====product Add a buffer ..., buffer:3=productor_th->54====product add a buffer ..., buffer:4//producers start writing buffers, When written to 4th (more than half) a notice to consumers =productor_th->58====notify consumer ..., Buffer:4=consumer_th->25====consumer consume a Buffer, buffer=3//consumer side consumption buffer =productor_th->54====product add a buffer ..., buffer:4//producers side production buffers =productor_th-> 58====notify consumer ..., buffer:4=productor_th->54====product add a buffer ..., buffer:5=productor_th->58==== Notify consumer ..., buffer:5=consumer_th->25====consumer consume a buffer, buffer=4=productor_th->54==== Product Add a buffer ..., buffer:5=productor_th->58====notify consumer ..., buffer:5=productor_th->54==== Product Add a buffer ..., buffer:6=productor_th->58====notify consumer ..., buffer: 6=productor_th->49=====productor wait consumer to consume, start sleep ..., buffer=6//when the producer production buffer is full, start sleeping =consumer_ Th->25====consumer consume a buffer, buffer=5=consumer_th->25====consumer consume a buffer, buffer=4=consumer_ Th->25====consumer consume a buffer, buffer=3=consumer_th->25====consumer consume a buffer, buffer=2// When consumer spending buffers are less than half. Wake-up producers start production =consumer_th->28====notify productor ..., buffer:2=productor_th->54====product add a buffer ..., Buffer:3=productor_th->54====product Add a buffer ..., buffer:4=productor_th->58====notify consumer ..., buffer : 4=productor_th->54====product Add a buffer ..., buffer:5=productor_th->58====notify consumer ..., buffer:5= Consumer_th->25====consumer consume a buffer, buffer=4=productor_th->54====product add a buffer ..., buffer:5= Productor_th->58====notify consumer ..., buffer:5=productor_th->54====product add a buffer ..., buffer:6= Productor_th->58====notify consumer ..., buffer:6=productor_th-> 49=====productor wait consumer to consume, start sleep ..., buffer=6 
This section source code download: Click to open the link

Linux mutual exclusion and synchronization applications (iii): POSIX threading implements a single producer and a single consumer model

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.