"Copyright Notice: respect for the original, reproduced please retain the source: blog.csdn.net/shallnet or .../gentleliu, the article is for learning communication only, do not use for commercial purposes"In the first section, we talk about producer consumer issues, and this section lets us make a slightly modified model: the initial buffer is empty, the producer writes data to the buffer, the consumer sleeps in the case of the buffer is empty, when the producer writes half the buffer and notifies the consumer that it can start consuming, the producer begins to sleep, Until the consumer consumes half of the buffer and notifies the producer to start production, where producers and consumers access the buffer mutually exclusive. Here's 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 the buffer is increased to half, inform consumers to start consuming the 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 is the output of the program running:
=consumer_th->20=====consumer wait productor to product, start sleep ..., buffer=0//consumers start sleeping wait for producer 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 producer production buffers are 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 reach less than half, wake-up producers start producing =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
SOURCE download: Click to open the link
Linux mutual exclusion and synchronization applications (iii): POSIX threading implements a single producer and a single consumer model