boost::lockfree::queue
#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的更多相关文章
- boost::lockfree::queue多线程读写实例
最近的任务是写一个多线程的东西,就得接触多线程队列了,我反正是没学过分布式的,代码全凭感觉写出来的,不过运气好,代码能够work= = 话不多说,直接给代码吧,一个多消费者,多生产者的模式.假设我的任 ...
- boost::lockfree::spsc_queue
#include <boost/thread/thread.hpp> #include <boost/lockfree/spsc_queue.hpp> #include < ...
- Boost lockfree deque 生产者与消费者多对多线程应用
boost库中有一个boost::lockfree::queue类型的 队列,对于一般的需要队列的程序,其效率都算不错的了,下面使用一个用例来说明. 程序是一个典型的生产者与消费者的关系,都可以使用多 ...
- Boost Lockfree
Boost Lockfree flyfish 2014-9-30 为了最大限度的挖掘并行编程的性能考虑使用与锁无关的数据结构来编程 与锁无关的数据结构不是依赖于锁和相互排斥来确保线程安全. Lockf ...
- boost::lockfree::stack
#include <boost/thread/thread.hpp> #include <boost/lockfree/stack.hpp> #include <iost ...
- boost 无锁队列
一哥们翻译的boost的无锁队列的官方文档 原文地址:http://blog.csdn.net/great3779/article/details/8765103 Boost_1_53_0终于迎来了久 ...
- lockfree buffer test
性能测试(3): 对无锁队列boost::lockfree::queue和moodycamel::ConcurrentQueue做一个性能对比测试 版权声明:本文为博主zieckey原创文章, ...
- Boost无锁队列
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/okiwilldoit/article/details/50970408 在开发接收转发agent时, ...
- c++ freelockquque
http://www.boost.org/doc/libs/1_56_0/doc/html/boost/lockfree/queue.html Class template queue boost:: ...
随机推荐
- onbeforepaste事件用法
onkeyup="value=value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text ...
- 2015 Multi-University Training Contest 1记录
1001 OO's Sequence 分析: 对于例子,能够得到,我们要求的是(1,1)(1,2)(1,3)(1,4)(1,5)(2,2)(2,3)(2,4)(2,5)(3,3)(3,4)(3,5)( ...
- HAWQ技术解析(四) —— 启动停止
前面已经完毕了HAWQ的安装部署,也了解了HAWQ的系统架构与主要组件,以下開始使用它. HAWQ作为Hadoop上的一个服务提供给用户,与其他全部服务一样.最主要的操作就是启动.停止 ...
- ssh中使用spring的集成quartz 编写定时任务
之前没有使用框架开发时对于开发定时任务都是 使用java的原声timer类,重写线程的run方法跑要执行的任务.刚刚换的新公司,项目使用ssh2,目前该项目中的定时任务的使用spirng集成的quar ...
- SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 这个 ...
- selenium从入门到应用 - 7,testNG的DataProvider
本系列所有代码 https://github.com/zhangting85/simpleWebtest 本文将介绍一个Java+TestNG+Maven+Selenium的web自动化测试脚本环境下 ...
- MySql常用函数数学函数、加密函数等(转—收藏)
MySql函数众多,这里只是列举了一部分常用的函数. 一.数学函数 ABS(x) // 返回x的绝对值 BI ...
- Python趣味实用小工具
代码地址如下:http://www.demodashi.com/demo/12918.html python 趣味实用小工具 概述 用python实现的三个趣味实用小工具: 图片转Execl工具 , ...
- PHP-CURL在POST请求时的注意事项
今天搞12306抢票, 在用CURL模拟POST请求校验验证码时, 无论如何12306都返回零, 正常应该返回True或者False, 最后查找原因如下 只是请求头 Content-Type 用了 a ...
- .NET 环境中使用RabbitMQ RabbitMQ与Redis队列对比 RabbitMQ入门与使用篇
.NET 环境中使用RabbitMQ 在企业应用系统领域,会面对不同系统之间的通信.集成与整合,尤其当面临异构系统时,这种分布式的调用与通信变得越发重要.其次,系统中一般会有很多对实时性要求不高的 ...