我有如下代码
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 (...)
{
}
}
我正在尝试优雅地退出该过程。但问题是我很少看到该程序在加入时挂起。
线程在无限 while 循环中仍然处于活动状态。即使在调用 stop 之后 m_alive 值也是“true”(在 stop 中将其设置为 false)。m_alive 被声明为 volatile。