基于Boost.Asio的异步编程

// TestASIO.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include 
#include 
#include 
#include 

// 一般的数据传输过程需要通过函数的返回值来判断数据传输是否成功。Boost Asio将数据传输分为两个独立的步骤:
// 
// 采用异步任务的方式开始数据传输。
// 将传输结果回掉通知调用端

void myPrint(const boost::system::error_code& /*e*/)
{
    std::cout << "[" << std::this_thread::get_id() << "]:Hello World!\n";
}

//参数绑定
void myPrint_new(const boost::system::error_code& /*e*/,
    boost::asio::steady_timer* t, int* count)
{
    if (*count < 5)
    {
        std::cout << *count << std::endl;
        ++(*count);
		//将计时器的到期时间从之前的到期时间移动一秒。
	    //通过相对于旧的过期时间计算新的过期时间,我们可以确保计时器不会因为处理处理程序的任何延迟而偏离整秒标记。
		t->expires_at(t->expiry() + boost::asio::chrono::seconds(1));
		t->async_wait(boost::bind(myPrint_new, boost::asio::placeholders::error, t, count));
    }  
}

//使用成员函数作为处理程序
//我们没有像在前面的教程程序中那样定义一个空闲函数print作为回调处理程序,而是定义了一个名为printer的类。
//该类的构造函数将引用io_context对象,并在初始化timer_成员时使用它。用来关闭程序的计数器现在也是类的成员。
class printer
{
public:
    printer(boost::asio::io_context& io) 
        : timer_(io, boost::asio::chrono::seconds(1))
        , count_(0) 
    {
        timer_.async_wait(boost::bind(&printer::print, this));
    }
    ~printer() 
    {
        std::cout << "Final count is " << count_ << std::endl;
    }
protected:
    void print()
    {
        if (count_ < 5)
        {
            std::cout << count_ << std::endl;
            ++count_;
            timer_.expires_at(timer_.expiry() + boost::asio::chrono::seconds(1));
            timer_.async_wait(boost::bind(&printer::print, this));
        }
    }

private:
    boost::asio::steady_timer timer_;
    int count_;
};

//在多线程程序中同步处理器
class PrintNew
{
public:
    PrintNew(boost::asio::io_context& io)
        : strand_(boost::asio::make_strand(io))
        , timer1_(io, boost::asio::chrono::seconds(1))
        , timer2_(io, boost::asio::chrono::seconds(1))
        , count_(0)
    {
        timer1_.async_wait(boost::asio::bind_executor(strand_, boost::bind(&PrintNew::print1, this)));
        timer2_.async_wait(boost::asio::bind_executor(strand_, boost::bind(&PrintNew::print2, this)));
    }
     ~PrintNew()
    {
        std::cout << "Final count is " << count_ << std::endl;
    }

protected:
    void print1()
    {
        if (count_ < 10)
        {
            std::cout << "Timer 1: " << count_ << std::endl;
            ++count_;
            timer1_.expires_at(timer1_.expiry() + boost::asio::chrono::seconds(1));
            timer1_.async_wait(boost::asio::bind_executor(strand_, boost::bind(&PrintNew::print1, this)));
        }
    }

    void print2()
    {
        if (count_ < 10)
        {
            std::cout << "Timer 2: " << count_ << std::endl;
            ++count_;
            timer2_.expires_at(timer2_.expiry() + boost::asio::chrono::seconds(1));
            timer2_.async_wait(boost::asio::bind_executor(strand_, boost::bind(&PrintNew::print2, this)));
        }
    }


private:
    boost::asio::strand strand_;
    boost::asio::steady_timer timer1_;
    boost::asio::steady_timer timer2_;
    int count_;
};

int main()
{
//     boost::asio::io_context io;
//     boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5));
//     t.wait();
    
//     boost::asio::io_context io;
//     boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5));
//     //使用ASIO的异步函数,需要一个回调函数,将在异步操作完成时被调用
//     //myPrint将在异步等待结束后调用
//     t.async_wait(&myPrint);
//     std::cout << "[" << std::this_thread::get_id() << "]:Main Finish!\n";
//     //ASIO框架下,只有调用了io_context::run()函数,异步等待的回调才会被调用
//     //当计时器过期或者异步回调完成时,run函数才会返回
//     io.run();

//     boost::asio::io_context io;
//     int iCount = 0;
//     boost::asio::steady_timer tm(io, boost::asio::chrono::seconds(1));
//     tm.async_wait(boost::bind(myPrint_new, boost::asio::placeholders::error, &tm, &iCount));
//     io.run();
//     std::cout << "[" << std::this_thread::get_id() << "]:Final count is " << iCount << std::endl;

//     boost::asio::io_context io;
//     printer p(io);
//     io.run();

    boost::asio::io_context io;
    PrintNew p(io);
    boost::thread t(boost::bind(&boost::asio::io_context::run, &io));
    io.run();
    t.join();

    return 0;
}

你可能感兴趣的:(c++,多线程,同步)