1. 程式人生 > >Boost中ASIO的一些用法-定時器

Boost中ASIO的一些用法-定時器

一、介紹

ASIO,基於作業系統的非同步機制,可有效避免多執行緒程式設計的諸多副作用。

目前主要關注於通訊方面,使用大量的類封裝了socket,提供更高層次的介面

二、使用不需要編譯,預設不支援SSL,要支援的話需要自己編譯OpenSSL。

三、用法Sample

1、定時器

#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/bind.hpp>
using namespace boost;

void async_timer_callback(asio::deadline_timer* async_timer, int async_count, const system::error_code& err)
{
	if (err)
		std::cout << "async_timer.async_wait err = " << err.message() << std::endl;
	else
	{
		std::cout << "async_timer async_count = " << async_count << " now expires expires_from_now = " << async_timer->expires_from_now() << std::endl;
		if (--async_count > 0)
		{
			async_timer->expires_at(async_timer->expires_at() + posix_time::milliseconds(500));
			async_timer->async_wait(boost::bind(async_timer_callback, async_timer, async_count, asio::placeholders::error));
		}
	}
}

int main()
{
	asio::io_service ios;
	// 同步等待
	asio::deadline_timer timer(ios, posix_time::seconds(2));
	std::cout << "timer expires_at = " << timer.expires_at() << " expires_from_now = "<<timer.expires_from_now()<<std::endl;

	timer.wait();
	std::cout << "timer now expires expires_from_now = " << timer.expires_from_now() << std::endl;

	// 非同步等待
	asio::deadline_timer async_timer(ios, posix_time::milliseconds(500));
	std::cout << "async_timer expires_at = " << async_timer.expires_at() << " expires_from_now = " << async_timer.expires_from_now() << std::endl;

	int async_count = 5;
	async_timer.async_wait(boost::bind(async_timer_callback, &async_timer, async_count, asio::placeholders::error));
	
	system::error_code err;
	ios.run(err);
	if (err)
	{
		std::cout << "ios.run err = " << err.message() << std::endl;
	}
	else
	{
		std::cout << "ios.run done " << std::endl;
	}
	return 0;
}

timer expires_at = 2016-Sep-25 08:19:35.318075 expires_from_now = 00:00:02
timer now expires expires_from_now = -00:00:00.000694
async_timer expires_at = 2016-Sep-25 08:19:35.820758 expires_from_now = 00:00:00.500000
async_timer async_count = 5 now expires expires_from_now = -00:00:00.000550
async_timer async_count = 4 now expires expires_from_now = -00:00:00.000476
async_timer async_count = 3 now expires expires_from_now = -00:00:00.000748
async_timer async_count = 2 now expires expires_from_now = -00:00:00.000643
async_timer async_count = 1 now expires expires_from_now = -00:00:00.000445
ios.run done