#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. lodash 集合处理方法 map和filter区别

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  2. C语言-srand种子详解

    rand() 函数取得随机数的时候是通过一个叫做"种子"的变量经过计算得出一个数值, 然后得出的数值再作为新的"种子"参与下一次的运算, 这样就得到了所谓的随机 ...

  3. 6、udev机制

        udev 机制,主要实现的是当设备连接系统的时候,在 /dev 目录下,自动创建设备节点.   1.1.工作方式     当设备连接或者移除的时候,内核会发出热拔插事件(hotplug eve ...

  4. xml文件的增删改读

    最近学习了利用XmlDocument对象对xml进行增删改读操作,就写了一个小的例子记录下来,加深印象,以后忘了也可以找出来看看. xml文件: <?xml version="1.0& ...

  5. python使用单例模式创建MySQL链接

    代码: from functools import wraps import mysql.connector from sshtunnel import SSHTunnelForwarder def ...

  6. CSDN开源夏令营 百度数据可视化实践 ECharts(4)

    ECharts知识点总结: 在应用过程中总会遇到一些难以理解的概念和属性,这里就总结了一下比較难的知识点,方便理解概念.进而更好的掌握ECharts. (1)1.  一个完整的option包括什么?能 ...

  7. opencv中各种矩阵乘的差别

    尊重原创,转载请注明:http://blog.csdn.net/tangwei2014 OpenCV中每次遇到矩阵乘法就乱,各种翻各种查. 这次总结了一下.为了简单明了,还是让样例说话. 1. Mat ...

  8. python装饰器的理解

    学习python,发现装饰器是一个比较难理解的地方. 下面用代码来说明. 装饰器的作用是为了切面编程(AOP).这种编程在java上有很多实现方式.下面直接说明吧: 1.作为装饰器的函数至少有两个de ...

  9. socket编程之实现简单的ssh

    服务器代码: #-*- coding:utf-8 -*- #edited by python3.6 # import socket,os ''' 创建socket对象 ''' server = soc ...

  10. Jenkins安装和配置系列(阳光温暖了心情)

    转自:http://www.cnblogs.com/yangxia-test/category/668771.html Jenkins学习一:Jenkins是什么? Jenkins学习二:Jenkin ...