开发者

asio::io_service hangs on destruction

i have timer implemented with boost (144 -> newest lib). This timer is started within a boos thread. The timer also starts a thread in which an io_service hosts deadline timer until thread is terminated. so a continuous timer. The boost thread which needs the timer is create from within DLL.

The DLL ExitInstance function is called, when the hosting App. unloads the DLL. The ExitInstance function stops the thread and before timer. But the io_service destruction in my timer never returns, so the app hangs.

This happens NOT, when i am able to call a Dispose Function before ExitInstance is called. However, some application loading my DLL, give not the chance to call this expose function.

Anyone know, how to work around this problem ?

Here is the code of my timer core. It is the thread which restarts the service until thre开发者_运维百科ad is stopped. The thread get's stopped by setting _stop flag and setting deadlime timer to 1 ms in future. summary: hangs when within Dll::ExitInstance destructed. Hangs not, if destrcuted before Dll::ExitInstance Thank you

void tcTimerThread::timerLoop(void)
{
        _running=true;

        /// create asynch service
        _io_service = new boost::asio::io_service;

        /// create timer
        _pTimer = new boost::asio::deadline_timer(*_io_service);
        while(_stop==false)
        {   
            _pTimer->expires_from_now(boost::posix_time::milliseconds(_delay));            
            /// bind timer event function
            _pTimer->async_wait(boost::bind(&tcTimerThread::timerEvent,this,boost::asio::placeholders::error));     
            try
            {
                if(_stop==false)
                {
                    /// reset async
                    _io_service->reset();
                    /// wait for timer event
                    _io_service->run(); 
                }
            }
            catch(...)
            {
            }
        }
        try
        {
           _io_service->stop();
           delete _pTimer;
           delete _io_service;
        }
        catch(...)
        {
        }
        _running=false;
}


You should be able to add DllMain to your DLL in order to reliably receive notification of thread- and process-level attach and detach. Perform required cleanup in process detach.

Only unknown to me is that ref to ExitInstance implies that this is an MFC app, and I have not done this in that scenario. I think this is the most reliable way to handle DLL-specific resources though.

EDIT:

MSDN docs imply that you should be able to override ExitInstance so that you can clean up your resources before the base MFC implementation gets called. is this feasible for you?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜