C++11 implementation of producer consumer issues

Source: Internet
Author: User

Producer consumer problem is a very classic problem in multithreading concurrency. I am here to implement a c++11-based, single-producer single-consumer version for your reference.

#include <windows.h> #include <iostream> #include <cstdlib> #include <mutex> #include < thread> #include <condition_variable>const int buffersize=10;struct itemrepository{int item_buffer[  Buffersize];int Write_pos;   Current position of Producerint Read_pos; Current position of consumerstd::condition_variable not_empty_con;std::condition_variable Not_full_con;std::mutex MTX;} Itemrepository;void Produce_one_item (Itemrepository*ir) {if (!ir) return;int* Item_buffer = ir->item_buffer;std:: unique_lock<std::mutex> Lock (IR-&GT;MTX); if (Ir->write_pos = = ir->read_pos&&item_buffer[ir- >write_pos] = = 1)//the repository is full{std::cout << "the producer was waiting for an empty." << Std::endl ; ir->not_full_con.wait (lock);}  Item_buffer[ir->write_pos] = 1;//now The buffer in this position is Fullstd::cout << "the producer produces a new Item on "<< Ir->write_pos << std::endl;ir->write_pos++;ir->not_Empty_con.notify_all ();//notify the consumer to consume itemsif (Ir->write_pos = = buffersize) Ir->write_pos = 0;} void Consume_one_item (Itemrepository*ir) {if (!ir) return;int* Item_buffer = ir->item_buffer;std::unique_lock< std::mutex> Lock (IR-&GT;MTX); if (Ir->read_pos = = Ir->write_pos&&item_buffer[ir->write_pos] = = 0) The repository is empty{std::cout << "The consumer are waiting for items." << Std::endl;ir->not_empty_con. Wait (lock);} Item_buffer[ir->read_pos] = 0;//now The buffer in this position was Emptystd::cout << "the consumer consumes an IT Em on "<< ir->read_pos << std::endl;ir->read_pos++;ir->not_full_con.notify_all ();//notify the Producer to produce new itemsif (Ir->read_pos = = buffersize) Ir->read_pos = 0;} void Producetask () {for (int i = 0; i <; i++) Produce_one_item (&itemrepository);} void Consumetask () {for (int i = 0; i <; i++) {Sleep (1); Consume_one_item (&itemrepository);}} void inItializerepository (Itemrepository*ir) {if (!ir) return;ir->read_pos = 0;ir->write_pos = 0;} int main () {initializerepository (&itemrepository); Std::thread producer (producetask); Std::thread Consumer ( Consumetask);p roducer.join (); Consumer.join (); System ("pause"); return 0;}

Notice that I determine whether the items in the Item_buffer are empty or full: the position of the producer and the consumer is equal, if the position is empty then buffer is empty, and if full, the buffer is full.

C++11 implementation of producer consumer issues

Related Article

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.