Producer Consumer Issues

Source: Internet
Author: User

Producer Consumer Issues (English:producer-consumer problem), also known as limited buffering problems (English:bounded-buffer problem), is a classic case of a multithreaded synchronization problem. The problem describes two threads that share fixed-size buffers-the so-called "producer" and "consumer"-problems that can occur when they actually run. The primary role of the producer is to generate a certain amount of data into the buffer, and then repeat the process. At the same time, consumers consume the data in buffers. The key to this issue is to ensure that producers do not add data when the buffer is full, and consumers do not consume data when the buffer is empty

One implementation is as follows:

#include <stdio.h>#include<unistd.h>#include<pthread.h>#include<string.h>#defineMAX 5//the size of the bufferpthread_mutex_t Mutex=pthread_mutex_initializer;pthread_cond_t Cond=Pthread_cond_initializer;typedefstruct{    CharBuffer[max]; intcount;} Buffer; Buffer Share= {"",0};CharCH ='A';void*producer (void*Arg) {printf ("producer:starting \ n");  while(ch! ='K') {Pthread_mutex_lock (&mutex); if(Share.count! =MAX) {Share.buffer[share.count+ +] = ch++; printf ("producer:put char[%c]\n", ch-1); if(Share.count = =MAX) {printf ("producer:signaling full\n"); Pthread_cond_signal (&cond);//If the cache is full, send a signal}} pthread_mutex_unlock (&mutex); } Sleep (1); printf ("produce:exiting \ n"); Pthread_exit (NULL);}void*consumer (void*junk) {    inti; printf ("consumer:starting\n");  while(ch! ='K') {Pthread_mutex_lock (&mutex); printf ("\ consumer:waiting\n");  while(Share.count! =MAX) {pthread_cond_wait (&cond, &mutex);//The condition does not establish a release lock.printf"Consumer wating for full signal\n"); } printf ("consumer:getting buffer::");  for(i =0; Share.buffer[i] && share.count;++i, share.count--) Putchar (Share.buffer[i]); Putchar ('\ n'); Pthread_mutex_unlock (&mutex); }}intMain () {pthread_t read, write; Pthread_create (&read, NULL, (void*) consumer, NULL); Pthread_create (&write, NULL, (void*) producer, NULL);    Pthread_join (read, NULL);    Pthread_join (write, NULL); return 0;}
View Code

To modify the consumer code:

void*consumer (void*junk) {    inti; printf ("consumer:starting\n");  while(ch! ='K') {Pthread_mutex_lock (&mutex); printf ("\ consumer:waiting\n"); //While (share.count! = MAX) {Pthread_cond_wait (&cond, &mutex);//The condition does not establish a release lock.printf"Consumer wating for full signal\n"); //}printf"consumer:getting buffer::");  for(i =0; Share.buffer[i] && share.count;++i, share.count--) Putchar (Share.buffer[i]); Putchar ('\ n'); Pthread_mutex_unlock (&mutex); }}

There are two kinds of results when compiling a run:

One is:

consumer:starting Consumer:WaitingProducer:starting producer:putChar[A]producer:putChar[B]producer:putChar[C]producer:putChar[D]producer:putChar[e]producer:signaling fullconsumer wating forFull signalconsumer:getting buffer:: ABCDE Consumer:WaitingProducer:putChar[F]producer:putChar[G]producer:putChar[H]producer:putChar[I]producer:putChar[j]producer:signaling fullconsumer wating forFull signalconsumer:getting buffer:: fghijproduce:exiting

The other is:

Char Char char char char [E] Producer:signaling fullconsumer:starting     consumer:waiting

It can be seen that the second is to execute the producer first, the producer fills in the buffer, sends the condition message, but at this time the consumer has not executed, also did not wait for the condition

Then the producer releases the lock, then the consumer acquires the lock, and then waits for the condition, because the buffer is full,

if (Share.count = = MAX)

The producer does not enter the code that sends the message, so the consumer waits for the condition.

And the result of the above is that the consumption is executed first, into the waiting condition, so there is no problem, but the problem is that we cannot guarantee that pthread_cond_wait () must be executed first with pthread_cond_signal ().

However, by adding the while () code, there is no problem waiting for the condition variable, and you can execute the following code directly.

The solution for this producer consumer is that at the same time there is only one producer and consumer, which is 1:1 of the relationship, the production is finished and then the consumer comes to fetch, the consumer and the producer during their respective activities, the other side is waiting, this efficiency is lower.

Producer Consumer Issues

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.