(1) 基于boost的生产者/消费者队列

template<typenameData>
classconcurrent_queue { private: std::queue<Data> the_queue; mutableboost::mutex the_mutex; boost::condition_variable the_condition_variable; public: void push(Data const& data) { boost::mutex::scoped_lock lock(the_mutex); the_queue.push(data); lock.unlock(); the_condition_variable.notify_one(); } bool empty() const { boost::mutex::scoped_lock lock(the_mutex); returnthe_queue.empty(); } bool try_pop(Data& popped_value) { boost::mutex::scoped_lock lock(the_mutex); if(the_queue.empty()) { returnfalse; } popped_value=the_queue.front(); the_queue.pop(); returntrue; } void wait_and_pop(Data& popped_value)
{ boost::mutex::scoped_lock lock(the_mutex); while(the_queue.empty()) { the_condition_variable.wait(lock); } popped_value=the_queue.front(); the_queue.pop(); } };

  

CUtilityCode的更多相关文章

随机推荐

  1. python操作日期和时间的方法

    不管何时何地,只要我们编程时遇到了跟时间有关的问题,都要想到 datetime 和 time 标准库模块,今天我们就用它内部的方法,详解python操作日期和时间的方法.1.将字符串的时间转换为时间戳 ...

  2. [NOIP2016]换教室 D1 T3 Floyed+期望DP

    [NOIP2016]换教室 D1 T3 Description 对于刚上大学的牛牛来说, 他面临的第一个问题是如何根据实际情况中情合适的课程. 在可以选择的课程中,有2n节课程安排在n个时间段上.在第 ...

  3. 双系统win+ubuntu无法访问win的盘符

    1.打开终端:如果没有安装ntfs-3g就要安装: sudo apt-get install ntfs-3g 2.修复挂载错误的相应的分区: sudo ntfsfix /dev/sda(×) (×)取 ...

  4. Windows OS上安装运行Apache Kafka教程

    Windows OS上安装运行Apache Kafka教程 下面是分步指南,教你如何在Windows OS上安装运行Apache Zookeeper和Apache Kafka. 简介 本文讲述了如何在 ...

  5. Spring中scope作用域

    scope作用域: 1.prototype 2.request      3.session 4.singleton 5.global session 1.prototype(多例) prototyp ...

  6. css 注意点

    HTML css 一.整体布局 1.创建一个html标签 2.创建三个div标签(分别是网页的头部,中间,和底部三部分) 3.一般都用class选择器 4.用css给body标签加个 margin:0 ...

  7. java和js根据一个或者多个空格截取字符串

    java: String str = "张三 fw1234"; String s[] = str.split("\\s+"); js: var str=&quo ...

  8. 手把手教你编写Logstash插件

    使用过Logstash的朋友都知道,它强大的插件生态几乎覆盖了所有的开源框架.从基本的http.tcp.udp.file,到强大的kafa.redis.ganglia,还有丰富的解析工具,比如date ...

  9. shape

    <?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http:/ ...

  10. javascript:算法之for循环

    一.for中的表达式 ①短路表达式 a && b是表达式:当执行的时候如果表达式a结果不是0,就会执行表达式b.如果表达式a执行的结果是0,表达式b不执行 ②i++与++i的不同 ++ ...