|
Boost Users : |
Subject: [Boost-users] [ASIO] When the work object is destroyed?
From: Daniele Barzotti (daniele.barzotti_at_[hidden])
Date: 2010-02-16 06:19:42
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
in my library I create a work object on the heap to maintain the
io_service running and I assign it to a object member shared_ptr so,
when the last object is destroyed, the work ends and the
io_service.run() returns.
All works but, if when I create an object after the thread is ended, the
io_service returns immediately..
..and I don't know why, any suggestions?
Thanks!
Daniele.
- -------------------------------------------------------------------
// 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;
void io_worker_thread(void)
{
#if(WIN32 && _WIN32_DCOM)
struct com_init
{
com_init() { CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); }
~com_init() { CoUninitialize(); }
} initializer_object;
#endif
io_service.run();
thread_started = false;
};
EXTERN_C EUROATLIB_API Hndl CALL GetTETRAMotoObj()
{
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:
// Instead of start the worker thread directly by passing the
// io_service object,
// I call the io_worker_thread function where I can do some
// initialization tasks like calling CoInitializeEx for COM
io_service_thread.reset(new boost::thread(io_worker_thread));
thread_started = !thread_started;
} catch (boost::thread_resource_error e) {
// Failed to create the new thread
return 0;
}
}
// create the new object
TETRAMoto* pTETRAMoto = new TETRAMoto(io_service);
// Assign a reference to io_service work object:
// This reference is used to counting the objects created.
// When the last TETRAMoto object is deleted the main work object
// is destroyed and the io_service thread ends.
pTETRAMoto->_io_work_ptr.reset(p_work);
// return the object
return pTETRAMoto;
};
- -------------------------------------------------------------------
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
iEYEARECAAYFAkt6f04ACgkQ/l+kMioSZwjYiwCgleSrLLiKKQlmZjN7HQBSEs75
olUAoKhMEV7/SK6tiI4hIxUoRjQv9qEE
=KGSy
-----END PGP SIGNATURE-----
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