#include <boost/thread/thread.hpp>
#include <boost/lockfree/stack.hpp>
#include <iostream> #include <boost/atomic.hpp> boost::atomic_int producer_count();
boost::atomic_int consumer_count(); boost::lockfree::stack<int> stack();//多生产者多消费者无锁栈 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 (!stack.push(value))
;
}
} boost::atomic<bool> done (false); void consumer(void)
{
int value;
while (!done) {
while (stack.pop(value))
++consumer_count;
} while (stack.pop(value))
++consumer_count;
} int main(int argc, char* argv[])
{
using namespace std;
cout << "boost::lockfree::stack is ";
if (!stack.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::stack的更多相关文章

  1. Boost Lockfree

    Boost Lockfree flyfish 2014-9-30 为了最大限度的挖掘并行编程的性能考虑使用与锁无关的数据结构来编程 与锁无关的数据结构不是依赖于锁和相互排斥来确保线程安全. Lockf ...

  2. boost::lockfree::queue多线程读写实例

    最近的任务是写一个多线程的东西,就得接触多线程队列了,我反正是没学过分布式的,代码全凭感觉写出来的,不过运气好,代码能够work= = 话不多说,直接给代码吧,一个多消费者,多生产者的模式.假设我的任 ...

  3. boost::lockfree::spsc_queue

    #include <boost/thread/thread.hpp> #include <boost/lockfree/spsc_queue.hpp> #include < ...

  4. boost::lockfree::queue

    #include <boost/thread/thread.hpp> #include <boost/lockfree/queue.hpp> #include <iost ...

  5. Boost lockfree deque 生产者与消费者多对多线程应用

    boost库中有一个boost::lockfree::queue类型的 队列,对于一般的需要队列的程序,其效率都算不错的了,下面使用一个用例来说明. 程序是一个典型的生产者与消费者的关系,都可以使用多 ...

  6. 使用C++11实现无锁stack(lock-free stack)

    前几篇文章,我们讨论了如何使用mutex保护数据及使用使用condition variable在多线程中进行同步.然而,使用mutex将会导致一下问题: 等待互斥锁会消耗宝贵的时间 — 有时候是很多时 ...

  7. 并发编程(三): 使用C++11实现无锁stack(lock-free stack)

    前几篇文章,我们讨论了如何使用mutex保护数据及使用使用condition variable在多线程中进行同步.然而,使用mutex将会导致一下问题: 等待互斥锁会消耗宝贵的时间 - 有时候是很多时 ...

  8. 并发编程入门(三): 使用C++11实现无锁stack(lock-free stack)

    前几篇文章,我们讨论了如何使用mutex保护数据及使用使用condition variable在多线程中进行同步.然而,使用mutex将会导致一下问题: 等待互斥锁会消耗宝贵的时间 - 有时候是很多时 ...

  9. boost 无锁队列

    一哥们翻译的boost的无锁队列的官方文档 原文地址:http://blog.csdn.net/great3779/article/details/8765103 Boost_1_53_0终于迎来了久 ...

随机推荐

  1. C#秘密武器之泛型

    一.简介: 很多初学者在刚开始接触泛型的时候会比较难理解泛型,在这里先把 “泛型”当作一个形容词,这样就方便理解了,因为很多东西都可以是泛型的!比如:“泛型的类”,“泛型的方法”,“泛型的接口”,“泛 ...

  2. Ubuntu14.04使用samba服务器共享Home目录

    这里记录一下,以Ubuntu 14.04为例.   1.安装samba服务器. sudo apt-get install samba 2.修改配置文件 sudo vim /etc/samba/smb. ...

  3. html的小例子

    常用的前端实例: 1略 2.在网页商城中的图片当我们把鼠标放上去之后,图片会显示一个有颜色的外边框,图片某一部分的字体的颜色并发生改变 鼠标放上去之前 鼠标放上去之后: 实现的代码: <!DOC ...

  4. Crontab命令--Linux

    Crontab命令--定时任务   命令格式 Example:  

  5. SDWebImage源代码解析(一)

    一.概念 SDWebImage是一个开源的第三方库,它提供了UIImageView的一个分类.以支持从远程server下载并缓存图片的功能. 二.优势 自从iOS5.0開始.NSURLCache也能够 ...

  6. Spring事务的传播行为 @Transactional

    Spring事务的传播行为http://blog.csdn.net/cuker919/article/details/5957209 在service类前加上@Transactional,声明这个se ...

  7. redis 大内存主从问题解决

    redis 内存太大使用太大会导致同步陷入循环,每次rdb还没同步完成,新的同步又起,解决办法: redis-cli config set client-output-buffer-limit &qu ...

  8. oracle 11g 中 (oracle 10g) crsctl 的 替换命令

     oracle 11g 中 (oracle 10g) crsctl 的 替换命令 Deprecated Command Replacement Commands crs_stat  ---集群状态 ...

  9. Atitit.手机验证码的破解---伪随机数

    Atitit.手机验证码的破解---伪随机数 1. 手机验证码几乎都是伪随机数1 2. 伪随机数1 2.1. 生成方法编辑1 2.2. 随机数的计算方法在不同的计算机中是不同的,即使在相同的计算机中安 ...

  10. 使用apxs安装apache模块

    使用apxs安装apache模块                 ---by石锅拌饭 1.缘由 前几天迁移系统.发现配置了fastcgi的一个脚本下载文件总是提示类似Connection reset ...