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 ...
随机推荐
- 正则表达式表示 ja.resx 所在行
[^\n]*ja.resx[^\n]*\n?正则表达式表示 ja.resx 所在行 用ultraEdit 删除关键字所在行的下一行或是上一行,所在行保留 删除 关键字所在行 的前3行: (^.*?(\ ...
- bzoj1710【Usaco2007 Open】Cheappal 便宜回文
1710: [Usaco2007 Open]Cheappal 便宜回文 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 466 Solved: 262 ...
- Django——如何使用Template以及如何向template传递变量
如何使用模板系统 在Python代码中使用Django模板的最基本方式如下: 可以用原始的模板代码字符串创建一个 Template 对象, Django同样支持用指定模板文件路径的方式来创建 Temp ...
- FileZilla_Server如何配置
安装采用默认直接安装. 2.下图直接点OK 进入主界面,如下 3.配置账户 选择 Edit->USERS 开始创建账户 点击ADD 按钮添加账户user1 点OK 后,回到创建账 ...
- mui 根据 json 数据动态创建列表
使用 underscore.js 模块解析 Underscore提供了一个轻量级的模板解析函数,它可以帮助我们有效地组织页面结构和逻辑. 实例: <!DOCTYPE html> <h ...
- Vue 前端路由 vue-router
1.前端路由 后端路由:多页面,服务器端渲染好返回给浏览器. 前端路由:改变url不向服务器发送请求:前端可以监听url变化:前端可以解析url并执行相应操作. 前后端分离:后端只提供API来返回数据 ...
- 对象内部属性[[Class]]
1.概述 所有的typeof返回值为‘object’的对象都包含一个内部属性[[Class]],我们将它可以看做内部的分类,而非传统面向对象意义的分类.这个属性无法直接访问,一般通过Object.pr ...
- iOS蓝牙原生封装,助力智能硬件开发
代码地址如下:http://www.demodashi.com/demo/12010.html 人工智能自1956年提出以来,一直默默无闻,近年来人工智能的发展得到重视逐渐发展起步,智能硬件.智能手环 ...
- C-类型转换(陷阱)
getchar() 返回值为int类型 1.自动类型转换(运算符两边变量类型不同时) 1).两个变量类型自动转换成一样的类型(会根据参数类型自动转换, 而不是直接位转换), 且运算结果也是转换后的类型 ...
- 将 ASP.NET Core 2.0 项目升级至 ASP.NET Core 2.1
主要升级步骤如下: 将 .csproj 项目文件中的 target framework 改为 netcoreapp2.1 <TargetFramework>netcoreapp2.1< ...