#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();
}

最新文章

  1. 探索c#之递归APS和CPS
  2. zlog学习笔记(zc_hashtable)
  3. microstrip(微带线)、stripline(带状线) 指什么?
  4. Web测试Selenium:如何选取元素
  5. Mac下sublime text 的“package control”安装
  6. django基本命令备忘录
  7. win7 安装 redis +php扩展
  8. C#整理3——运算符和语句
  9. poj 2417 Discrete Logging(A^x=B(mod c),普通baby_step)
  10. shell中$(( )) 与 $( ) 还有${ }的区别
  11. Sublime Text编辑远程Linux服务器上的文件
  12. Linux 下的一个全新的性能测量和调式诊断工具 Systemtap, 第 2 部分: DTrace
  13. [ionic3.x开发记录]ng-content使用
  14. dubbo could not get local host ip address will use 127.0.0.1 instead 异常处理
  15. 【NPM】设置代理
  16. WebSphere部署应用教程
  17. 03 Linux的目录结构与常见文件管理
  18. EasyExcel 解析excel
  19. [iOS问题归总]iPhone上传项目遇到的问题
  20. linux记录sftp命令

热门文章

  1. 获取 input 输入框的值
  2. UnrealEngine4.5 BluePrint初始化中遇到编译警告的解决办法
  3. React Native :加载新闻列表
  4. js中的 return false;
  5. 使用sql语句实现添加、删除约束
  6. jQuery框架源码解读
  7. 第四天 ThinkPHP手把手高速拼接站点(四)
  8. in App Purchases一个注意事项
  9. centos/rhel下实现nginx自启动脚本实例
  10. mongo aggregate