jeudi 18 octobre 2018

boost::asio, cannot get destructor messages after Ctrl-C signal

I would like to know why I am not getting the destructor messages when I stop this program with Ctrl-C. Also is it possible to have two different timers in the same io service ( as it is shown in the class printer? ).

Code is below it is compiled with "g++ f.cpp -lboost_system -std=c++14"

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

class printer
{
public:
  printer(boost::asio::io_service& io) : timer1_(io, boost::posix_time::seconds(1)), timer2_(io, boost::posix_time::seconds(1))
  {
    print();
    do1();
  }

  ~printer()
  {
    std::cout << "printer stops \n";
  }

  void do1 () {

      std::cout << "do1" << "\n";

      timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(10));
      timer2_.async_wait(boost::bind(&printer::do1, this));

  }

  void print()
  {
      std::cout << "print" << "\n";

      timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(5));
      timer1_.async_wait(boost::bind(&printer::print, this));
  }

private:
  boost::asio::deadline_timer timer1_;
  boost::asio::deadline_timer timer2_;
};

class Test {
public:
    Test() {

        printer p(io);
        io.run();
    }   

    ~Test() {
        std::cout << "let's stop the printer" << std::endl;
        io.stop();
    } 

boost::asio::io_service io;

};

int main()
{
    auto a1 = std::make_shared<Test>();
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire