基于boost 线程并行技术实现消息队列方式
来源:互联网 发布:网络投资平台黑名单 编辑:程序博客网 时间:2024/05/22 05:31
小立再也没有机会,就这样失去,人生当中最大的悲哀莫过于此。才发现原来生活就是这么的槽糕,所有的都是没有劲的。
翻遍csdn,也翻遍博客园,更翻遍google 中文,翻遍英文只有两篇。就以此文献给这些堕落的,颓废的,乱七八槽的日子吧。
#include <queue>#include <boost/asio.hpp>#include <boost/bind.hpp>#include <boost/thread.hpp>#include <boost/thread/tss.hpp>#include <boost/thread/mutex.hpp>#include <boost/thread/condition.hpp>#include <boost/date_time/posix_time/posix_time.hpp>#include <iostream>using namespace std;using namespace boost;boost::recursive_mutex io_mutex;boost::condition_variable_any cond;std::queue<int> iq;class printer{public: printer(boost::asio::io_service& io,int n) : strand_(io), timer1_(io, boost::posix_time::seconds(1)), timer2_(io, boost::posix_time::seconds(1)), count_(n) { timer2_.async_wait(boost::bind(&printer::enqueue, this)); timer1_.async_wait(boost::bind(&printer::dequeue, this)); } ~printer() { boost::recursive_mutex::scoped_lock lock(io_mutex); std::cout << "Final count is " << count_ << "\n"; } void dequeue() { boost::recursive_mutex::scoped_lock lock(io_mutex); while(iq.empty()) { cond.wait(lock); } int pop =0; if (!iq.empty()) { pop = iq.front(); iq.pop(); cout<<"------------pop "<<pop<<endl; } cond.notify_all(); timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1)); timer1_.async_wait(boost::bind(&printer::dequeue, this)); } void enqueue() { boost::recursive_mutex::scoped_lock lock(io_mutex); while(iq.size() == 1000) { cond.wait(lock); } iq.push(count_);; cond.notify_all(); cout<<"------------push "<<count_<<endl; timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1)); timer1_.async_wait(boost::bind(&printer::enqueue, this)); }private: boost::asio::strand strand_; boost::asio::deadline_timer timer1_; boost::asio::deadline_timer timer2_; int count_;};int main(){ thread_group threads; boost::asio::io_service io; printer * p[1000]; int num = 1000; for(int i = 0; i < num; i++) { p[i] = new printer(io,i); threads.create_thread(boost::bind(&boost::asio::io_service::run, &io)); } io.run(); threads.join_all(); std::system("pause"); return 0;}
- 基于boost 线程并行技术实现消息队列方式
- 基于boost 线程并行技术实现消息队列方式[记录]
- OpenStack消息队列AMQP技术具体实现 [分析基于icehouse]
- 使用boost条件变量实现消息队列
- 利用线程消息队列,实现任务队列
- boost ipc 消息队列
- 基于PHP使用rabbitmq实现消息队列
- SpringBoot 基于Redis快速实现消息队列
- 通过消息队列实现线程间通讯
- 简单的线程消息队列实现
- 协议与其消息队列实现方式
- 基于java自身技术实现消息方式的系统间通信
- 基于环形队列的消息队列的实现 (转贴CSDN)
- 基于boost 的苹果apns消息推送实现(1)
- 用消息的方式实现线程通信
- 基于Boost线程库实现Java中atomic…
- 消息队列技术终结者(四)—消息消费者以何种方式接收消息
- 线程与消息队列
- 一个很好的MyBatis入门示例程序
- android webservice ksoap 报错 org.xmlpull.v1.XmlPullParserException: expected: START_TAG 的解决办法
- ubuntu apache小记
- C++异常处理
- Visual Studio 2010 代码探索系列课程
- 基于boost 线程并行技术实现消息队列方式
- 关于fflush(stdin)的误区
- 打开注册表编辑器的方法及常见问题解决
- 如何使Winform的Textbox滚动条保持在最后
- myeclipse 8.5 注册码
- JQuey 介绍(1)
- 时间计算
- 03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-1)
- 多核处理器的九大关键技术