#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. Alfresco 5.0.d 企业文档管理系统

    Thanks for downloading Alfresco Community Edition. Your download should begin in three seconds. If i ...

  2. Node.js meitulu图片批量下载爬虫1.03版

    //====================================================== // https://www.meitulu.com图片批量下载Node.js爬虫1. ...

  3. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-点击运行按钮进入到运行状态报错Error starting TwinCAT System怎么办 AdsWarning1823怎么办

    一般提示如下   点击Device,然后选中当前真正连接到的网卡   一般是由于重装系统之后,没有把本来是realtime capable的设备Install,所以找不到支持EtherCAT的设备导致 ...

  4. iOS 之UIBezierPath

    代码地址如下:http://www.demodashi.com/demo/11602.html 在之前的文章中,由于用到过UIBezierPath这个类,所以这里就对这个类进行简单的记录一下,方便自己 ...

  5. Centos下安装JDK、Maven和Git

    原文地址:https://github.com/eacdy/spring-cloud-book/blob/master/3%20%E4%BD%BF%E7%94%A8Docker%E6%9E%84%E5 ...

  6. 转:VLC搭建RTSP直播流,图文介绍

    将一个视频转成rtsp流,通过vlc播放器,搭建一个rtsp服务器,让rtsp客户端去访问这个视频的rtsp流 1 需要有vlc播放器,我的版本如下 2  媒体 -->  流 3  添加视频文件 ...

  7. 在连接mysql数据库时出错:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone

    这个错误是时区导致的,所以需要在配置连接url后面加上时区: url=jdbc:mysql://localhost:3309/test?serverTimezone=UTC 其中UTC是统一标准世界时 ...

  8. gm picture

    console.log("ok") /*var images = require("images");var fs = require("fs&quo ...

  9. RAD Studio XE5破解补丁及方法

    通过测试可用,RAD Studio XE5破解补丁及方法 第一步,将下载下来的“delphicbuilder_xe5_win.iso”解压到任意盘,任意目录. 第二步,将“免序列号安装授权文件”文件夹 ...

  10. linux系统之free命令详解

    total used free shared buffers cached Mem: -/+ buffers/cache: Swap: 上面是free命令的执行结果,下面我来详细说说其中的含义: Me ...