生产者消费者问题是多线程并发中一个非常经典的问题。我在这里实现了一个基于C++11的,单生产者单消费者的版本,供大家参考。

#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <mutex>
#include <thread>
#include <condition_variable>
const int bufferSize=;
struct ItemRepository
{
int item_buffer[bufferSize];
int write_pos; //current position of producer
int read_pos; //current position of consumer
std::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->mtx);
if (ir->write_pos == ir->read_pos&&
item_buffer[ir->write_pos] == )//the repository is full
{
std::cout << "The producer is waiting for an empty." << std::endl;
ir->not_full_con.wait(lock);
} item_buffer[ir->write_pos] = ;//now the buffer in this position is full
std::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 items if (ir->write_pos == bufferSize)
ir->write_pos = ;
} void consume_one_item(ItemRepository*ir)
{
if (!ir)
return;
int* item_buffer = ir->item_buffer;
std::unique_lock<std::mutex> lock(ir->mtx); if (ir->read_pos == ir->write_pos&&
item_buffer[ir->write_pos] == )//the repository is empty
{
std::cout << "The consumer is waiting for items." << std::endl;
ir->not_empty_con.wait(lock);
} item_buffer[ir->read_pos] = ;//now the buffer in this position is empty
std::cout << "The consumer consumes an item on " << ir->read_pos << std::endl;
ir->read_pos++;
ir->not_full_con.notify_all();//notify the producer to produce new items if (ir->read_pos == bufferSize)
ir->read_pos = ;
} void produceTask()
{
for (int i = ; i < ; i++)
produce_one_item(&itemRepository);
} void consumeTask()
{
for (int i = ; i < ; i++)
{
Sleep();
consume_one_item(&itemRepository);
}
} void initializeRepository(ItemRepository*ir)
{
if (!ir)
return;
ir->read_pos = ;
ir->write_pos = ;
} int main()
{
initializeRepository(&itemRepository);
std::thread producer(produceTask);
std::thread consumer(consumeTask);
producer.join();
consumer.join();
system("pause");
return ;
}

注意我判断item_buffer中的物品是否全空或者全满的条件:生产者和消费者的位置相等时,若该位置上为空则buffer全空,若为满则buffer全满。

C++11实现生产者消费者问题的更多相关文章

  1. C++11 实现生产者消费者双缓冲

    基础的生产者消费者模型,生产者向公共缓存区写入数据,消费者从公共缓存区读取数据进行处理,两个线程访问公共资源,加锁实现数据的一致性. 通过加锁来实现 class Produce_1 { public: ...

  2. C++11 实现生产者消费者模式

    代码都类似,看懂一个,基本都能理解了. 共有代码: #include <cstdlib>#include <condition_variable>#include <io ...

  3. C++11 并发指南九(综合运用: C++11 多线程下生产者消费者模型详解)

    前面八章介绍了 C++11 并发编程的基础(抱歉哈,第五章-第八章还在草稿中),本文将综合运用 C++11 中的新的基础设施(主要是多线程.锁.条件变量)来阐述一个经典问题——生产者消费者模型,并给出 ...

  4. 综合运用: C++11 多线程下生产者消费者模型详解(转)

    生产者消费者问题是多线程并发中一个非常经典的问题,相信学过操作系统课程的同学都清楚这个问题的根源.本文将就四种情况分析并介绍生产者和消费者问题,它们分别是:单生产者-单消费者模型,单生产者-多消费者模 ...

  5. 再谈多线程模型之生产者消费者(总结)(c++11实现)

    0.关于 为缩短篇幅,本系列记录如下: 再谈多线程模型之生产者消费者(基础概念)(c++11实现) 再谈多线程模型之生产者消费者(单一生产者和单一消费者)(c++11实现) 再谈多线程模型之生产者消费 ...

  6. 再谈多线程模型之生产者消费者(多生产者和多消费者 )(c++11实现)

    0.关于 为缩短篇幅,本系列记录如下: 再谈多线程模型之生产者消费者(基础概念)(c++11实现) 再谈多线程模型之生产者消费者(单一生产者和单一消费者)(c++11实现) 再谈多线程模型之生产者消费 ...

  7. 再谈多线程模型之生产者消费者(多生产者和单一消费者 )(c++11实现)

    0.关于 为缩短篇幅,本系列记录如下: 再谈多线程模型之生产者消费者(基础概念)(c++11实现) 再谈多线程模型之生产者消费者(单一生产者和单一消费者)(c++11实现) 再谈多线程模型之生产者消费 ...

  8. 再谈多线程模型之生产者消费者(单一生产者和多消费者 )(c++11实现)

    0.关于 为缩短篇幅,本系列记录如下: 再谈多线程模型之生产者消费者(基础概念)(c++11实现) 再谈多线程模型之生产者消费者(单一生产者和单一消费者)(c++11实现) 再谈多线程模型之生产者消费 ...

  9. 再谈多线程模型之生产者消费者(单一生产者和单一消费者)(c++11实现)

    0.关于 为缩短篇幅,本系列记录如下: 再谈多线程模型之生产者消费者(基础概念)(c++11实现) 再谈多线程模型之生产者消费者(单一生产者和单一消费者)(c++11实现)[本文] 再谈多线程模型之生 ...

随机推荐

  1. ftpget 从Windows FTP服务端获取文件

    /********************************************************************************* * ftpget 从Windows ...

  2. jdbc mysql写入中文乱码解决

    一. 问题 数据库编码:utf8 mysql> create database dbnameDEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ...

  3. data process for large scale datasets

    Kmeans:   总体而言,速度(单线程): yael_kmeans > litekmeans ~ vl_kmeans 1.vl_kemans (win10 + matlab 15 + vs1 ...

  4. http 学习 1-2 chapter2-URL与资源

    URL是因特网资源的标准化名称.URL指向每一条电子信息,告诉他们位于何处,以及如何与之进行交互. URL语法,以及各种URL组件的含义及其所做的工作. 很多Web客户端都支持额URL快捷方式,包括相 ...

  5. e

    frame问题 以下面的frame为例: ? 1 <frame  src=”xxx.html”  id=”frameId”  name=”frameName”  /> (1)访问frame ...

  6. React 学习笔记(学习地址汇总)

    好的博文地址:http://www.ruanyifeng.com/blog/2015/03/react.html 官网学习地址:http://facebook.github.io/react/docs ...

  7. python 实现文件下载

    Requests库,高度封装的http库 import requests url = 'http://down.sandai.net/thunder9/Thunder9.0.18.448.exe' f ...

  8. JAVA要死了吗?不!我来告诉你为什么!

    我们看到"Java 死了吗?" 这个问题,年年都被抛出来,然而至今为止,从所有的第三方统计来看,Java 不仅活的很好,还在保持增长.虽然不断有新的语言面世,TIOBE 仍将 Ja ...

  9. sql join,left join,rigt join

    left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录.right join :右连接,返回右表中所有的记录以及左表中连接字段相等的记录.inner join: 内连接,又叫等 ...

  10. HTTP Code

    所有 HTTP 状态代码及其定义. 代码  指示  2xx  成功  200  正常:请求已完成.  201  正常:紧接 POST 命令.  202  正常:已接受用于处理,但处理尚未完成.  20 ...