Boost logo

Boost Users :

Subject: [Boost-users] [ASIO] How to terminate a io_service inside a library.
From: Daniele Barzotti (daniele.barzotti_at_[hidden])
Date: 2009-04-10 06:48:00


Hi,

I'm sorry if this is not a question specifically about ASIO, but I need
a suggestion..

I have a library that can be built like a Dynamic or static.

Inside I have the following code:

----------------------------------------------------------
// 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 work(io_service);
static bool thread_started;

EXTERN_C EUROATLIB_API MyObjHandl CALL GetMyObj()
{
  if (!thread_started) {
    try {
      // 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;
    } catch (boost::thread_resource_error e) {
      // Failed to create the new thread
      return 0;
    }
  }
  // create the new object
  return new MyObj(io_service);

};
----------------------------------------------------------

Ok, now I need a way to stop the io_service thread when all MyObj are
destroyed.

In the clients I use:

boost::scoped_ptr<MyObj> _devMyObj( GetMyObj() );

How can I accomplish this in a right way?

Thanks,
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