[ASIO] How to terminate a io_service inside a library.

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_ptrboost::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.

// Main io_service static boost::asio::io_service io_service; static boost::scoped_ptrboost::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?
I think the best way to do this is to remove the "work" object when you want the io_service's thread(s) to end. So, if you wish this to happen on last MyObj destruction, I guess the most intuitive approach would be to create "work" on the heap and give each MyObj a shared_ptr to it - then everything would happen automatically. The only concern here is the fact that "work" is destroyed (and thus detached from the io_service) in a thread other than io_service's one. But since io_service is stated to be thread-safe, I guess it should be safe.

Igor R wrote:
I think the best way to do this is to remove the "work" object when you want the io_service's thread(s) to end. So, if you wish this to happen on last MyObj destruction, I guess the most intuitive approach would be to create "work" on the heap and give each MyObj a shared_ptr to it - then everything would happen automatically. The only concern here is the fact that "work" is destroyed (and thus detached from the io_service) in a thread other than io_service's one. But since io_service is stated to be thread-safe, I guess it should be safe.
Thanks a lot Igor. Only one thing.. Is there a way to retrieve a work object from the io_service? Now I done it like: EXTERN_C EUROATLIB_API MyObjHandl CALL GetMyObj() { ... p_work = new boost::asio::io_service::work(io_service); // create the new object MyObj pMyObj = new MyObj(io_service); pMyObj->io_work_ptr = p_work; return pMyObj; } But I would like to hide the assignment doing it automatically inside my MyObj which take an io_service ref in the constructor. Thanks! Daniele.

Only one thing.. Is there a way to retrieve a work object from the io_service?
No, I don't think it's possible - at least, because you can create as many "work"'s as you wish - io_service doesn't care about it (as far as I understand).
participants (2)
-
Daniele Barzotti
-
Igor R