boost::lockfree::spsc_queue
#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的更多相关文章
- Boost Lockfree
Boost Lockfree flyfish 2014-9-30 为了最大限度的挖掘并行编程的性能考虑使用与锁无关的数据结构来编程 与锁无关的数据结构不是依赖于锁和相互排斥来确保线程安全. Lockf ...
- boost::lockfree::queue多线程读写实例
最近的任务是写一个多线程的东西,就得接触多线程队列了,我反正是没学过分布式的,代码全凭感觉写出来的,不过运气好,代码能够work= = 话不多说,直接给代码吧,一个多消费者,多生产者的模式.假设我的任 ...
- boost::lockfree::stack
#include <boost/thread/thread.hpp> #include <boost/lockfree/stack.hpp> #include <iost ...
- boost::lockfree::queue
#include <boost/thread/thread.hpp> #include <boost/lockfree/queue.hpp> #include <iost ...
- Boost lockfree deque 生产者与消费者多对多线程应用
boost库中有一个boost::lockfree::queue类型的 队列,对于一般的需要队列的程序,其效率都算不错的了,下面使用一个用例来说明. 程序是一个典型的生产者与消费者的关系,都可以使用多 ...
- 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时, ...
- 项目中的Libevent(多线程)
多线程版Libevent //保存线程的结构体 struct LibeventThread { LibEvtServer* that; //用作传参 std::shared_ptr<std::t ...
随机推荐
- 微信小程序 - 文本框显示限制最大长度
wxml <view class='textarea-count'> <textarea placeholder='请输入文字' bindinput="getWords&q ...
- ibatis--百度百科
iBATIS一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2002年发起的开放源代码项目.于2010年6月16号被谷歌托管,改名为MyBatis.是一个基 ...
- iOS 使用AFNetWorking监听APP网络状态变化(可用于更改缓存策略、提示网络等)
前言 我们知道在APP开发过程中.监听手机当前的网络状态还是一个非经常常使用的方法,这里我来为大家接受一种使用AFNetWorking来监听当前的网络状态的方法:网络监听对程序开发的帮助有非常多:比方 ...
- 基于Opencv自带BP网络的车标简易识别
代码地址如下:http://www.demodashi.com/demo/12966.html 记得把这几点描述好咯:代码实现过程 + 项目文件结构截图 + 演示效果 1.准备工作 1.1 训练集和测 ...
- 电脑端的全能扫描王:图片转文字识别、识别pdf、图片中的文字,图片提取txt
手机中有全能扫描王,但PC端没有.所以需要另外找. 发现微软的oneNode有提供类似的功能. 第一步.下载Microsoft OneNode http://www.onenote.com/downl ...
- 获取文本中你须要的字段的 几个命令 grep awk cut tr sed
1,grep 2,awk 3,cut 4,tr 5,sed 实例1 获取本地IP地址 /sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v ine ...
- Python归并排序(递归实现)
为什么归并排序如此有用?1. 快捷和稳定归并排序成为⼀一个非常棒的排序算法主要是因为它的快捷和稳定.它的复杂度即使在最差情况下都是O(n log n).而快速排序在最差情况下的复杂度是O(n^2),当 ...
- Git实战(四)状态转换
上次的Git实战(三)环境搭建博文.我们大致解说了一下git的环境安装,今天我们解说一下Git的状态转换. 学习版本号控制工具.对工具进行版本号控制之间的状态转换很重要.毕竟Git仅仅是一个工具.假设 ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...