#include <boost/thread/thread.hpp>
#include <boost/lockfree/queue.hpp>
#include <iostream> #include <boost/atomic.hpp> boost::atomic_int producer_count();
boost::atomic_int consumer_count(); boost::lockfree::queue<int> queue();//无锁的多生产者多消费者队列 const int iterations = ;
const int producer_thread_count = ;
const int consumer_thread_count = ; void producer(void)
{
for (int i = ; i != iterations; ++i) {
int value = ++producer_count;
while (!queue.push(value))
;
}
} boost::atomic<bool> done (false);
void consumer(void)
{
int value;
while (!done) {
while (queue.pop(value))
++consumer_count;
} while (queue.pop(value))
++consumer_count;
} int main(int argc, char* argv[])
{
using namespace std;
cout << "boost::lockfree::queue is ";
if (!queue.is_lock_free())
cout << "not ";
cout << "lockfree" << endl; boost::thread_group producer_threads, consumer_threads;//线程组 for (int i = ; i != producer_thread_count; ++i)
producer_threads.create_thread(producer); for (int i = ; i != consumer_thread_count; ++i)
consumer_threads.create_thread(consumer); producer_threads.join_all();
done = true; consumer_threads.join_all(); cout << "produced " << producer_count << " objects." << endl;
cout << "consumed " << consumer_count << " objects." << endl;
getchar();
}

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

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

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

  2. boost::lockfree::spsc_queue

    #include <boost/thread/thread.hpp> #include <boost/lockfree/spsc_queue.hpp> #include < ...

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

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

  4. Boost Lockfree

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

  5. boost::lockfree::stack

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

  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. c++ freelockquque

    http://www.boost.org/doc/libs/1_56_0/doc/html/boost/lockfree/queue.html Class template queue boost:: ...

随机推荐

  1. CentOS 7 yum安装失败问题

    在CentOS 7中,执行yum安装,一直报错,错误信息如下 其实在上述的错误信息中,上述中的repodata/repomd.xml文件据说是/mnt目录rpm包的目录,路径 在/mnt中因为没有/r ...

  2. linux上MySQL改动password的各种方法,yc整理

    MySQL改动password的各种方法 整理了下面四种在MySQL中改动rootpassword的方法,可能对大家有所帮助! 方法1: 用SET PASSWORD命令 mysql -uroot my ...

  3. 为Linux上FireFox安装Flash插件

    废话少说,步骤如下: 1.点击网页上插件缺失处,根据提示下载tar.gz版本的插件,我下载的版本是install_flash_player_11_linux.i386.tar.gz,这个文件被下载到了 ...

  4. 小米6安装google play

    http://bbs.xiaomi.cn/t-13579116 http://m.mk52.cn/jiaocheng/3288.html 步骤: 1.下载需要的文件并解压 (http://techta ...

  5. Python学习笔记(四)多进程的使用

    python中多进程与Linux 下的C基本相同.   fork的基本使用   先看最简单的例子: # coding: utf-8 import os def my_fork(): pid = os. ...

  6. 微信小程序页面跳转

    一:跳转的数据传递 例如:wxml中写了一个函数跳转: [html] view plain copy <view class="itemWeight" catchtap=&q ...

  7. 自定义radio/checkbox样式

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. mysql调用存储过程出现Illegal mix of collations错误

    执行sql语句正常 执行存储过程 异常 提示 Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMP ...

  9. Atitit.  Js 冒泡事件阻止 事件捕获   事件传递  事件代理

    Atitit.  Js 冒泡事件阻止 事件捕获   事件传递  事件代理   1. 事件冒泡1 2. 事件捕获1 3. 同时支持了事件捕获阶段和事件冒泡阶段ddEventListener的第三个参数1 ...

  10. atitit.Atitit.检测文本文件的编码 java  与php版  。Net

    atitit.Atitit.检测文本文件的编码 java  与php版  .Net 1 检测编码原理 Utf8>>gbk 在此转会gbk>>utf 2 工具检测编码 不能使用l ...