也就是说dispatch和post最根本的区别是dispatch本身在条件允许的情况将会立刻执行handler方法,否则就会将其加入到工作队列等待上述四种方法的某一种来触发其执行;而post则总是将任务加入到工作队列。由于handler本身又可能往io_service的工作队列中注入任务,所以如果反复使用dispatch,poll有可能永远不会结束。如果采用post,则会正常结束。
下面是一个例子用来说明二者的不同:
#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则要等待队列中的其它完成后再执行;如果线程改为2个或更多,就是顺序输出了,这是因为有sleep的存在,去掉sleep后输出取决于谁先获得锁。如果工作有先后顺序,采用上面的方法显然是不能达到预期的。
解决上面的问题,boost提供了strand来给工作排序。strand能够保证handler有序执行,也就是说,如果我们通过strand给io_services post了work1->work2-->work3,不管有多少线程,它们都会按照那个顺序执行。下面是strand使用的一些规则,这对使用strand至关重要,如果我们不太了解,编写的代码可能会产生未定义的行为,导致运行很长时间,有时程序崩溃也很难排查。
规则:
如果有:strand s;满足完成处理要求的对象a,b;a,b的拷贝a1,b1;
当下面的任何之一条件成立:
- s.post(a)早于s.post(b)
- s.post(a)早于s.dispatch(b),后者在strand外进行
- s.dispatch(a)早于s.post(b),前者在strand外进行
- s.dispatch(a)早于s.dispatch(b),两者均在strand外进行
那么:asio_handler_invoke(a1,&a1)早于asio_handler_invoke(b1,&b1)
但是,如果像下面的情况:
async_op_1(..., s.wrap( a ));
async_op_2(..., s.wrap( b ));此时,这两个异步操作都会调用对应的dispatch,但是我们不知道dispatch(a)和dispach(b)的顺序。也就是说,上面任何一个条件都不满足,此时就不能决定二者的顺序。
下面是例子,注意,去掉了标准输出上的锁,采用多线程:
#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 PrintNum( int x ) { std::cout << "[" << boost::this_thread::get_id() << "] x: " << x << std::endl; } 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 ) ); boost::asio::io_service::strand strand( *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 < 4; ++x ) { worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) ); } boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) ); io_service->post( strand.wrap( boost::bind( &PrintNum, 1 ) ) ); io_service->post( strand.wrap( boost::bind( &PrintNum, 2 ) ) ); boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) ); io_service->post( strand.wrap( boost::bind( &PrintNum, 3 ) ) ); io_service->post( strand.wrap( boost::bind( &PrintNum, 4 ) ) ); boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) ); io_service->post( strand.wrap( boost::bind( &PrintNum, 5 ) ) ); io_service->post( strand.wrap( boost::bind( &PrintNum, 6 ) ) ); work.reset(); worker_threads.join_all(); return 0; }上面的strand并不能保证按需输出,改为
strand.post( boost::bind( &PrintNum, 1 ) );等形式就可以实现了。也就是说,在多线程中,如果我们采用下面的A类方法,可以保证顺序,采用B,则不能;这在strand的介绍文档中有专门的说明,是因为strand的post方法保序,而strand wrap到某个方法上并不能保证整体有序。