C++ 两个'io_service.run()'的功能是什么?
在boost asio教程中。我不知道它的功能C++ 两个'io_service.run()'的功能是什么?,c++,boost,boost-asio,C++,Boost,Boost Asio,在boost asio教程中。我不知道它的功能 boost::threadt(boost::bind(&boost::asio::io_service::run,&io))在主功能中 为什么要调用两个io_serice.run() // //timer.cpp // ~~~~~~~~~ // //版权所有(c)2003-2013 Christopher M.Kohlhoff(chris在Kohlhoff.com) // //根据Boost软件许可证1.0版发布。(见附页) //文件LICENSE
boost::threadt(boost::bind(&boost::asio::io_service::run,&io))代码>在主功能中
为什么要调用两个io_serice.run()
//
//timer.cpp
// ~~~~~~~~~
//
//版权所有(c)2003-2013 Christopher M.Kohlhoff(chris在Kohlhoff.com)
//
//根据Boost软件许可证1.0版发布。(见附页)
//文件LICENSE_1_0.txt或复制到http://www.boost.org/LICENSE_1_0.txt)
//
#包括
#包括
#包括
#包括
#包括
类打印机
{
公众:
打印机(boost::asio::io\U服务和io)
:钢绞线(io),
计时器1(io,boost::posix_time::seconds(1)),
定时器2(io,boost::posix_time::seconds(1)),
计数(0)
{
timer1.async\u wait(strand.wrap(boost::bind(&printer::print1,this));
timer2_u2;async_2;wait(strand_2;.wrap(boost::bind(&printer::print2,this));
}
~printer()
{
该示例演示了如何使用线程池(在本例中,由两个线程组成)。如果希望同时执行多个异步处理程序,则可能需要一个线程池
要引用链接的页面,请执行以下操作:
如果您发现自己遇到了这些限制,另一种方法是让一个线程池调用io_service::run()
要将线程提交到asio线程池,您需要在该线程上执行io_service::run
。此示例提交两个线程:boost::threadt
和主线程。本教程演示了如何通过调用线程池来并发执行处理程序。但是,本教程减少了对t的演示his通过在同一个处理程序中调度两个异步调用链来实现,其中的处理程序保证不会并发运行
尽管如此,文档建议如果应用程序遇到以下情况,使用一个线程池调用io\u service::run()
:
- 处理程序可能需要很长时间才能完成时响应性差
- 无法在多处理器系统上扩展
它使用两个线程来演示strand确保handler1和Handler2一个接一个地执行,但同时执行。如果我们使用一个线程,则无论我们使用与否,handler都将一个接一个地执行。
//
// timer.cpp
// ~~~~~~~~~
//
// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:
printer(boost::asio::io_service& io)
: strand_(io),
timer1_(io, boost::posix_time::seconds(1)),
timer2_(io, boost::posix_time::seconds(1)),
count_(0)
{
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
}
~printer()
{
std::cout << "Final count is " << count_ << "\n";
}
void print1()
{
if (count_ < 10)
{
std::cout << "Timer 1: " << count_ << "\n";
++count_;
timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
}
}
void print2()
{
if (count_ < 10)
{
std::cout << "Timer 2: " << count_ << "\n";
++count_;
timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
}
}
private:
boost::asio::strand strand_;
boost::asio::deadline_timer timer1_;
boost::asio::deadline_timer timer2_;
int count_;
};
int main()
{
boost::asio::io_service io;
printer p(io);
boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
io.run();
t.join();
return 0;
}