zoukankan      html  css  js  c++  java
  • 基于boost 线程并行技术实现消息队列方式[记录]

    #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;
    }
  • 相关阅读:
    fedora上部署ASP.NET——(卡带式电脑跑.NET WEB服务器)
    SQL Server 请求失败或服务未及时响应。有关详细信息,请参见事件日志或其它适合的错误日志
    8086CPU的出栈(pop)和入栈(push) 都是以字为单位进行的
    FTP 服务搭建后不能访问问题解决
    指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配
    Linux 安装MongoDB 并设置防火墙,使用远程客户端访问
    svn Please execute the 'Cleanup' command. 问题解决
    .net 操作MongoDB 基础
    oracle 使用绑定变量极大的提升性能
    尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题。
  • 原文地址:https://www.cnblogs.com/toosuo/p/2459764.html
Copyright © 2011-2022 走看看