【发布时间】:2014-10-11 09:44:51
【问题描述】:
据我了解,学习 boost asio 并找出一个名为“strand”的类。 如果只有一个 io_service 关联到特定 strand 并按 strand 发布句柄。
示例(来自here)
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 );
boost::thread_group worker_threads;
for( int x = 0; x < 2; ++x )
{
worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );
}
boost::this_thread::sleep( boost::posix_time::milliseconds( 1000 ) );
strand.post( boost::bind( &PrintNum, 1 ) );
strand.post( boost::bind( &PrintNum, 2 ) );
strand.post( boost::bind( &PrintNum, 3 ) );
strand.post( boost::bind( &PrintNum, 4 ) );
strand.post( boost::bind( &PrintNum, 5 ) );
然后 strand 将为我们序列化处理程序执行。但是这样做有什么好处呢?如果我们希望任务变为序列化?
【问题讨论】:
标签: c++ boost boost-asio