Boost logo

Boost Users :

Subject: [Boost-users] [asio] How to know if the io_service is runing
From: Daniele Barzotti (daniele.barzotti_at_[hidden])
Date: 2009-09-24 04:24:14


In my library I have this code (yet used in another project) where pwork
is assigned to a shared_ptr:

--------------------------------------------------------------
typedef boost::shared_ptr<boost::asio::io_service::work> io_work_ptr;

// Main io_service
static boost::asio::io_service io_service;
static boost::scoped_ptr<boost::thread> io_service_thread;
static boost::asio::io_service::work* p_work;
static bool thread_started;

bool CreateMainThread()
{
  if (!thread_started) {
    try {
      // create the work object on the heap
      p_work = new boost::asio::io_service::work(io_service);

      // run the IO service as a separate thread:
      io_service_thread.reset (
       new boost::thread (
         boost::bind (
           &boost::asio::io_service::run, &io_service
         )
       )
      );

      thread_started = !thread_started;
      return true;

    } catch (boost::thread_resource_error e) {
      // Failed to create the new thread
      return false;
    }
  }
}
--------------------------------------------------------------

WaveStream::WaveStream() : rx_timer_(io_service)
{
};

WaveStream::Test()
{
  //Start a new timer or renew it
  rx_timer_.expires_from_now( boost::posix_time::milliseconds(500) );
  // We managed to cancel the timer. Start new asynchronous wait.
  rx_timer_.async_wait(
    boost::bind(&WaveStream::handle_timeout, this, _1)
  );
}

void WaveStream::handle_timeout(const boost::system::error_code& error)
{
 ....
}
--------------------------------------------------------------

The timer callback is never called.
Is there a way to know if the io_service is runing?

Daniele.


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