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 ...
随机推荐
- Solution of NumberOfDiscIntersections by Codility
question:https://codility.com/programmers/lessons/4 this question is seem like line intersections qu ...
- shell学习小结
小结 本章我么介绍了怎样使用ls与stat露出文件与文件meta数据,还有怎样使用touch设置未见时间戳.touch可显示有关日期时间相关的信息以及在很多现行系统上的范围限制. 说明了怎样以shel ...
- 时间见证着—eternal life
上帝并不是这么公平的爱每个人,祂会多爱那些在困难.痛苦.悔恨中的人一点点.因为在那个当下,他们比一般人更需要知道上帝对他们的爱. 开通空间:http://imgcache.qq.com/qzone ...
- Java 8 List
排序 依据自定义对象的某个属性进行排序. List<Student> students = Arrays.asList(student1, student2, student3); stu ...
- android 完美的tabhost 切换多activity布局
TabHost在很多应用都会使用到,有时候在TabHost添加的Tab中设置view不能满足需求,因为在view中添加如PreferenceActivity相当困难. 之前在一个应用中需要实现使用Ta ...
- java之static关键字
介绍: 1.在类中,用static声明的成员变量为静态成员变量,它为该类的公用变量,在第一次使用时被初始化,对于该类的所有对象来说,static成员变量只有一份. 2.用static声明的方法为静态方 ...
- 由于删除DBF文件报错 —— ORA-01033: ORACLE initialization or shutdown in progress
由于移动或删除DBF文件报错:ORA-01033: ORACLE initialization or shutdown in progress 原因:一般该类故障通常是由于移动文件而影响了数据库日 ...
- vue 跨域:使用vue-cli 配置 proxyTable 实现跨域问题
路径在/config/index.js 中,找到dev.proxyTable.如下配置示例: proxyTable: { '/api': { // 我要请求的地址 target: 'http://oa ...
- Centos 7 部署FTP服务简单版
第三方教程推荐与参考: http://blog.csdn.net/somehow1002/article/details/70232791 先安装成功了,有信心了.再进一步扩展配置. 1.安装vsft ...
- linux下 目录(扩容)挂载磁盘
1.常用命令 查看硬盘的分区 #sudo fdisk -l 查看IDE硬盘信息 #sudo hdparm -i /dev/hda 查看STAT硬盘信息 #sudo hdparm -I /dev/sda ...