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