Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost asio deadline_timer, bug?
From: Boost lzw (boostlzw_at_[hidden])
Date: 2011-10-12 21:17:43


Hello, Allan,

Using boost::asio::io_service::stop() member function, you can
cancel/interrupt other handlers. The following code show that only one
handler (rather than 3 ones) would be executed:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
using namespace boost::asio;
class A {
public:
 A(io_service &is) : ios(is), timer1(ios), timer2(ios), timer3(ios) {
  timer1.expires_from_now(boost::posix_time::seconds(5));
  timer1.async_wait(boost::bind(&A::handler, this, _1));
  timer2.expires_from_now(boost::posix_time::seconds(1));
  timer2.async_wait(boost::bind(&A::interrupt, this, _1) );
 }
private:
 io_service &ios;
 deadline_timer timer1;
 deadline_timer timer2;
 deadline_timer timer3;

 void handler(const boost::system::error_code &e) {
  if (!e) {
   std::cout << "Forbidden handler" << std::endl;
  } else
   throw boost::system::error_code(e);
 }
 void interrupt(const boost::system::error_code &e) {
  if (!e) {
   std::cout << "Interrupted" << std::endl;
   ios.stop();
   timer3.expires_from_now(boost::posix_time::milliseconds(1) );
   timer3.async_wait(boost::bind(&A::handle_interrupt, this, _1) );

  } else
   throw boost::system::error_code(e);
 }
 void handle_interrupt(const boost::system::error_code &e) {
  if (!e) {
   std::cout << "Hellow world" << std::endl;
   } else
   throw boost::system::error_code(e);
 }
};
int main() {
 try {
  boost::asio::io_service ios;
  A a(ios);
  ios.run();
 } catch (std::exception &e ) {
  std::cout << e.what() << std::endl;
 }
 return 0;
}

Robert

On Fri, Aug 19, 2011 at 3:25 AM, Allan Nielsen <a_at_[hidden]> wrote:

> Hi
>
> Is this a bug, or am I doing something wrong?
>
> #include <iostream>
> #include <boost/asio.hpp>
> #include <boost/bind.hpp>
>
> using namespace std;
> using namespace boost::asio;
>
> struct A {
> A(io_service & service) : timer1(service), timer2(service) {
> timer1.expires_from_now( boost::posix_time::seconds(5) );
> timer1.async_wait(boost::bind(&A::handler, this, _1));
>
> timer2.expires_from_now( boost::posix_time::seconds(2) );
> timer2.async_wait(boost::bind(&A::interrupt, this, _1));
> }
>
> void handler(const boost::system::error_code &ec) {
> cout << "Forbidden handler" << endl;
> assert(0);
> }
>
> void handler_interrupted(const boost::system::error_code &ec) {
> cout << "Hello world" << endl;
> }
>
> void interrupt(const boost::system::error_code &ec) {
> cout << "Interrupted" << endl;
> int res = timer1.cancel();
> assert(res > 0);
>
> timer1.expires_from_now( boost::posix_time::milliseconds(0) );
> timer1.async_wait(boost::bind(&A::handler_interrupted, this, _1));
> }
>
> int count, max, timeout1, timeout2;
> deadline_timer timer1;
> deadline_timer timer2;
> };
>
> int main() {
> io_service service;
> A a(service);
> service.run();
>
> return 0;
> }
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net