#include <boost/thread/thread.hpp>
#include <boost/lockfree/spsc_queue.hpp>
#include <iostream> #include <boost/atomic.hpp> int producer_count = ;
boost::atomic_int consumer_count (); boost::lockfree::spsc_queue<int, boost::lockfree::capacity<> > spsc_queue;//但生产者但消费者队列,后面是指定其容量大小 const int iterations = ; void producer(void)
{
for (int i = ; i != iterations; ++i) {
int value = ++producer_count;
while (!spsc_queue.push(value))
;
}
} boost::atomic<bool> done (false); void consumer(void)
{
int value;
while (!done) {
while (spsc_queue.pop(value))
++consumer_count;
} while (spsc_queue.pop(value))
++consumer_count;
} int main(int argc, char* argv[])
{
using namespace std;
cout << "boost::lockfree::queue is ";
if (!spsc_queue.is_lock_free())
cout << "not ";
cout << "lockfree" << endl; boost::thread producer_thread(producer);//单生产者
boost::thread consumer_thread(consumer);//单消费者 producer_thread.join();
done = true;
consumer_thread.join(); cout << "produced " << producer_count << " objects." << endl;
cout << "consumed " << consumer_count << " objects." << endl;
getchar();
}

其实只要知道其实一种无锁队列是一种单生产者,单消费者的模式。

boost::lockfree::spsc_queue的更多相关文章

  1. Boost Lockfree

    Boost Lockfree flyfish 2014-9-30 为了最大限度的挖掘并行编程的性能考虑使用与锁无关的数据结构来编程 与锁无关的数据结构不是依赖于锁和相互排斥来确保线程安全. Lockf ...

  2. boost::lockfree::queue多线程读写实例

    最近的任务是写一个多线程的东西,就得接触多线程队列了,我反正是没学过分布式的,代码全凭感觉写出来的,不过运气好,代码能够work= = 话不多说,直接给代码吧,一个多消费者,多生产者的模式.假设我的任 ...

  3. boost::lockfree::stack

    #include <boost/thread/thread.hpp> #include <boost/lockfree/stack.hpp> #include <iost ...

  4. boost::lockfree::queue

    #include <boost/thread/thread.hpp> #include <boost/lockfree/queue.hpp> #include <iost ...

  5. Boost lockfree deque 生产者与消费者多对多线程应用

    boost库中有一个boost::lockfree::queue类型的 队列,对于一般的需要队列的程序,其效率都算不错的了,下面使用一个用例来说明. 程序是一个典型的生产者与消费者的关系,都可以使用多 ...

  6. boost 无锁队列

    一哥们翻译的boost的无锁队列的官方文档 原文地址:http://blog.csdn.net/great3779/article/details/8765103 Boost_1_53_0终于迎来了久 ...

  7. lockfree buffer test

    性能测试(3): 对无锁队列boost::lockfree::queue和moodycamel::ConcurrentQueue做一个性能对比测试     版权声明:本文为博主zieckey原创文章, ...

  8. Boost无锁队列

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/okiwilldoit/article/details/50970408 在开发接收转发agent时, ...

  9. 项目中的Libevent(多线程)

    多线程版Libevent //保存线程的结构体 struct LibeventThread { LibEvtServer* that; //用作传参 std::shared_ptr<std::t ...

随机推荐

  1. WCF报错

    1."没有终结点在侦听可以接受消息的 http://localhost:8084/Service1.svc.这通常是由于不正确的地址或者 SOAP 操作导致的.如果存在此情况,请参见 Inn ...

  2. JSP页面的基本结构 及声明变量

    一.JSP页面的基本结构 在传统的HTML页面文件里增加Java程序片和JSP标签就构成了一个JSP页面文件. 一个JSP页面可由5种元素组合而成: 1.普通的HTML标记符 2.Jsp标签.如指令标 ...

  3. js 队列和事件循环

    1.示例代码 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UT ...

  4. SQL优化- 数据库SQL优化——使用EXIST代替IN

    数据库SQL优化——使用EXIST代替IN 1,查询进行优化,应尽量避免全表扫描 对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引 . 尝试下面的 ...

  5. 使用 curl() 函数实现不同站点之间注册用户的同步

    一 需求 在A站点注册一个新用户,那么,在B站点也会被同时注册 二 思路 在A站点注册的同时,调用API接口实现在B站点也会被同时注册 三 实现 主要代码如下: function http_curl( ...

  6. Gherkin关键字

    Feature 功能 Background 背景 Scenario 场景 Outline Scenarios(or Examples) Given 假如.假设.假定 When 当 Then 那么 An ...

  7. node.js零基础详细教程(1):安装+基础概念

    第一章 建议学习时间2小时  课程共10章 学习方式:详细阅读,并手动实现相关代码 学习目标:此教程将教会大家 安装Node.搭建服务器.express.mysql.mongodb.编写后台业务逻辑. ...

  8. 转: Your build settings specify a provisioning profile with the UUID, no provisioning profile was found

    http://blog.csdn.net/rbyyyblog/article/details/12220875 在Archive项目时,出现了“Your build settings specify ...

  9. win10 rabbitMQ的安装与测试

    安装 1.首先,下载并运行Erlang for Windows 安装程序 (地址:http://www.erlang.org/downloads)下载完毕并安装(注意:安装目录请选择默认目录) 2.下 ...

  10. DropFileName = "svchost.exe" 问题解决方案

    1.至以下链接处下载ATTK扫描工具: http://support.trendmicro.com.cn ... stomizedpackage.exe (32位) http://support.tr ...