boost asio 学习

Posted itdef

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了boost asio 学习相关的知识,希望对你有一定的参考价值。

http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=4

 

本章节为io_service添加任务,并且区分dispatch与post的区别.如果说io_service是asio库的大脑,那么post与dispatch就是asio库的手和脚。

先看看示例1

#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <iostream>

boost::mutex global_stream_lock;

void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )
{
	global_stream_lock.lock();
	std::cout << "[" << boost::this_thread::get_id()
		<< "] Thread Start" << std::endl;
	global_stream_lock.unlock();

	io_service->run();

	global_stream_lock.lock();
	std::cout << "[" << boost::this_thread::get_id()
		<< "] Thread Finish" << std::endl;
	global_stream_lock.unlock();
}

size_t fib( size_t n )
{
	if ( n <= 1 )
	{
		return n;
	}
	boost::this_thread::sleep( 
		boost::posix_time::milliseconds( 1000 )
		);
	return fib( n - 1 ) + fib( n - 2);
}

void CalculateFib( size_t n )
{
	global_stream_lock.lock();
	std::cout << "[" << boost::this_thread::get_id()
		<< "] Now calculating fib( " << n << " ) " << std::endl;
	global_stream_lock.unlock();

	size_t f = fib( n );

	global_stream_lock.lock();
	std::cout << "[" << boost::this_thread::get_id()
		<< "] fib( " << n << " ) = " << f << std::endl;
	global_stream_lock.unlock();
}

int main( int argc, char * argv[] )
{
	boost::shared_ptr< boost::asio::io_service > io_service(
		new boost::asio::io_service
		);
	boost::shared_ptr< boost::asio::io_service::work > work(
		new boost::asio::io_service::work( *io_service )
		);

	global_stream_lock.lock();
	std::cout << "[" << boost::this_thread::get_id()
		<< "] The program will exit when all work has finished."
		<< std::endl;
	global_stream_lock.unlock();

	boost::thread_group worker_threads;
	for( int x = 0; x < 2; ++x )
	{
		worker_threads.create_thread( 
			boost::bind( &WorkerThread, io_service )
			);
	}

	io_service->post( boost::bind( CalculateFib, 3 ) );
	io_service->post( boost::bind( CalculateFib, 4 ) );
	io_service->post( boost::bind( CalculateFib, 5 ) );

	work.reset();

	worker_threads.join_all();

	return 0;
}

  代码使用智能指针控制io_service,使用mutex控制各个进程间的输出互斥。work类维持io_service的生命周期, 然后使用post添加执行任务。

 在此基础上我们再查看post与dispatch的区别:

post 优先将任务排进处理队列,然后返回,任务会在某个时机被完成。

dispatch会即时请求io_service去调用指定的任务。

示例2

#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <iostream>

boost::mutex global_stream_lock;

void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )
{
	global_stream_lock.lock();
	std::cout << "[" << boost::this_thread::get_id()
		<< "] Thread Start" << std::endl;
	global_stream_lock.unlock();

	io_service->run();

	global_stream_lock.lock();
	std::cout << "[" << boost::this_thread::get_id()
		<< "] Thread Finish" << std::endl;
	global_stream_lock.unlock();
}

void Dispatch( int x )
{
	global_stream_lock.lock();
	std::cout << "[" <<  boost::this_thread::get_id()  << "] " 
		<< __FUNCTION__  << " x = " << x <<  std::endl;
	global_stream_lock.unlock();
}

void Post( int x )
{
	global_stream_lock.lock();
	std::cout << "[" <<  boost::this_thread::get_id()  << "] " 
		<< __FUNCTION__  << " x = " << x <<  std::endl;
	global_stream_lock.unlock();
}

void Run3( boost::shared_ptr< boost::asio::io_service > io_service )
{
	for( int x = 0; x < 3; ++x )
	{
		io_service->dispatch( boost::bind( &Dispatch, x * 2 ) );
		io_service->post( boost::bind( &Post, x * 2 + 1 ) );
		boost::this_thread::sleep( boost::posix_time::milliseconds( 1000 ) );
	}
}

int main( int argc, char * argv[] )
{
	boost::shared_ptr< boost::asio::io_service > io_service( 
		new boost::asio::io_service
		);
	boost::shared_ptr< boost::asio::io_service::work > work(
		new boost::asio::io_service::work( *io_service )
		);

	global_stream_lock.lock();
	std::cout << "[" <<  boost::this_thread::get_id()  
		<< "] The program will exit when all  work has finished." <<  std::endl;
	global_stream_lock.unlock();

	boost::thread_group worker_threads;
	for( int x = 0; x < 1; ++x )
	{
		worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );
	}

	io_service->post( boost::bind( &Run3, io_service ) );

	work.reset();

	worker_threads.join_all();

	return 0;
}

  我们可以看到结果是先显示dispatch的结果然后才显示post结果,与预想的是一致的.

 

以上是关于boost asio 学习的主要内容,如果未能解决你的问题,请参考以下文章

boost asio 学习

boost.asio学习-----reslover 域名解析

boost asio 学习 定时器

boost asio 学习 错误处理

boost::asio 学习

boost asio 翻译