boost::asio阻塞等待与异步等待

阻塞等待:这里会等待3秒然后才进行输出。

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

int main(void)
{
	boost::asio::io_service io;
	boost::asio::deadline_timer timer(io, boost::posix_time::seconds(3));
	timer.wait();
	std::cout << "this is blocking wait" << std::endl;

	system("pause");
	return 0;
}

异步等待:需要一个回调函数,在等待这个print回调函数的时候程序会继续往下执行到io.run为止,3秒过后才会执行回调函数。如果在3秒内程序还没有执行到io.run(如加上sleep(5)),那么5秒后才会执行print,也就是说在执行io.run之前print是不会执行的。

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

void print(const boost::system::error_code & e)
{
	std::cout << "hello, world" << std::endl;
}

int main(void)
{
	boost::asio::io_service io;
	boost::asio::deadline_timer timer(io, boost::posix_time::seconds(3));
	timer.async_wait(&print);
	std::cout << "this is asynchronous wait" << std::endl;
        // sleep(5);
	io.run();

	system("pause");
	return 0;
}

// 开始timer的异步等待时间是1秒,1秒后会执行print回调函数,在这个函数里会进行5次递归调用。由于timer的
// 终止时间是1秒已经到期了所以在递归的过程中不会再每秒执行一次的,为了实现每秒输出的效果我们需要为这个
// timer延迟终止时间,expires_at就是其这个作用。当然expires_at必须在async_wait之前执行,否则是没有效果的。
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/bind.hpp>
#include <windows.h>

unsigned int start_t, end_t;
void print(const boost::system::error_code& e, boost::asio::deadline_timer* timer, int* count)
{
	if (*count < 5)
	{
		std::cout << "count: " << *count << std::endl;
		end_t = ::GetTickCount();
		std::cout << "elapse time:" << end_t-start_t << std::endl;
		++(*count);
                //timer->expires_from_now(boost::posix_time::seconds(1));
		timer->expires_at(timer->expires_at() + boost::posix_time::seconds(1));
		timer->async_wait(boost::bind(print, boost::asio::placeholders::error, timer, count));
	}
}

int main()
{
	int count = 0;
	boost::asio::io_service io;
	boost::asio::deadline_timer timer(io, boost::posix_time::seconds(1));
	timer.async_wait(boost::bind(print, boost::asio::placeholders::error, &timer, &count));
	start_t = ::GetTickCount();
	io.run();
	std::cout << "run end" << std::endl;

	system("pause");
	return 0;
}

// 下面是类成员函数回调的形式

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/bind.hpp>

class Printer
{
public:
	Printer(boost::asio::io_service& io)
		: count_(0), timer_(io, boost::posix_time::seconds(1))
	{
		timer_.async_wait(boost::bind(&Printer::print, this));
	}

	~Printer()
	{
	}

	void print()
	{
		if (count_ < 5)
		{
			std::cout << "count: " << count_ << std::endl;
			++count_;
			timer_.expires_from_now(boost::posix_time::seconds(1));
			timer_.async_wait(boost::bind(&Printer::print, this));
		}
	}

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

int main()
{
	boost::asio::io_service io;
	Printer printer(io);
	io.run();

	system("pause");
	return 0;
}


  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`boost::asio::io_service`是Boost库中的一个核心类,用于提供异步I/O操作的事件循环机制。它是实现异步网络编程的基础,可用于处理各种网络通信任务。 `boost::asio::io_service`的主要作用是管理和调度异步操作。它负责处理事件循环,监视和分发I/O事件,以及执行注册的回调函数。通过使用`io_service`对象,可以实现非阻塞式的异步I/O编程,提高程序的并发性和响应性。 以下是`boost::asio::io_service`类的常见用法示例: ```cpp #include <boost/asio.hpp> void handleRead(const boost::system::error_code& error, std::size_t bytes_transferred) { if (!error) { // 处理读取数据的回调逻辑 } else { // 处理错误情况 } } int main() { boost::asio::io_service io_service; // 创建一个socket对象 boost::asio::ip::tcp::socket socket(io_service); // 连接到服务器 socket.connect(endpoint); // 异步读取数据 boost::asio::async_read(socket, boost::asio::buffer(buffer), handleRead); // 开始事件循环 io_service.run(); return 0; } ``` 在上述示例中,首先创建了一个`io_service`对象,然后创建了一个TCP socket对象,并使用`io_service`对象进行异步读取操作。在`async_read`函数中,指定了一个回调函数`handleRead`,用于处理读取数据完成后的回调逻辑。 最后,通过调用`io_service`的`run()`函数启动事件循环,使程序进入等待事件的状态。在事件循环中,`io_service`会不断地监视和分发I/O事件,并执行注册的回调函数。 使用`boost::asio::io_service`,可以方便地实现异步的网络编程,处理各种I/O操作,如读取、写入、连接、接收等。它提供了高度灵活和可扩展的方式来构建异步应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值