生产者消费者问题是多线程并发中一个非常经典的问题。我在这里实现了一个基于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. IOS OC 多任务定时器 NSRunLoop 管理 NSTimer

    下面有两种做法 1.使用日期组件 NSDateComponents 2.使用NSString 生成一个日期 //  创建一个日历对象 NSCalendar *calendar = [NSCalenda ...

  2. MySQL开启general_log跟踪sql执行记录

    # 设置general log保存路径 # 注意在Linux中只能设置到 /tmp 或 /var 文件夹下,设置其他路径出错 # 需要root用户才有访问此文件的权限 mysql>set glo ...

  3. 配置不当导致无法加载odoo-10.0模块

    启动odoo-bin时出错 2017-01-05 06:38:51,046 5480 INFO ? odoo: Odoo version 10.02017-01-05 06:38:51,046 548 ...

  4. R包之间冲突带来的奇怪错误

    今天调试一个paper的代码,出现很奇怪的错误: qh2 <- mydf %>% filter(date >= as.Date('2013-08-14'),date <= as ...

  5. Struts2文件下载找不到输入流异常

    先发异常 Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack. Check ...

  6. delegate用法

    一般来说 delegate 可以申明一个delegate类型  比如 public delegate funa(object b) 然后使用的时候申明 funa 作为类型  new funa(回调函数 ...

  7. 数据注解和验证 – ASP.NET MVC 4 系列

           不仅在客户端浏览器中需要执行验证逻辑,在服务器端也需要执行.客户端验证能即时给出一个错误反馈(阻止请求发送至服务器),是时下 Web 应用程序所期望的特性.服务器端验证,主要是因为来自网 ...

  8. save_data

    <?php /** * * $model 实例化表模型 * * $url 跳转地址 */ public function save_data($model,$url){ if(false === ...

  9. [转]一些TCP和UDP使用问题汇总

    下面是在实际后台开发中遇到的一些关于TCP和UDP的问题,这里慢慢积累起来: UDP连续发送2次数据包,第一次发100字节,第二次发200字节,服务端recvfrom(1000)时收到100.200还 ...

  10. .NET的Cookie相关操作

    using System; using System.Collections.Generic; using System.Text; using System.Web; namespace Comm ...