Question

I have a code like below

void CLogThread::run()
{
    m_alive = True;  //only place where m_alive (declared volatile) set to true 

    while (m_alive)
    {
        //logic here
    }   
}


void CLogThread::stop()
{
    m_alive = False;
}




void CThreadManager::uninit() throw()
{
    try
    {
        if (m_pLogThread != NULL)
        {
            m_pLogThread->stop();
            (void)m_pLogThread->getThreadControl().join();
            m_pLogThread->uninit();
            m_pLogThread = NULL;    
        }

    }
    catch (...)
    {
    }
}

I am trying to exit the process gracefully. But the problem is very rarely i see that program hangs at join.

Thread is still active in infinite while loop. m_alive value is "true" even after stop is called (in stop its set to false). m_alive is declared as volatile.

Was it helpful?

Solution

Found the reason why m_alive is true.

Its because, when we create a thread (Generally calling "start" function) called is in different thread and it continued its execution. By the time "start" completes and calls stop, new thread has not scheduled yet. So "run" not started. When later run starts, reset the value of m_alive from false to true again....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top