Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 线程连接将永远等待:连接将永远挂起或等待_Multithreading_Pthreads_Pthread Join - Fatal编程技术网

Multithreading 线程连接将永远等待:连接将永远挂起或等待

Multithreading 线程连接将永远等待:连接将永远挂起或等待,multithreading,pthreads,pthread-join,Multithreading,Pthreads,Pthread Join,我有下面这样的代码 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:

我有下面这样的代码

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 (...)
    {
    }
}
我试图优雅地退出这个过程。但问题是我很少看到程序挂起在join上


线程在无限while循环中仍处于活动状态。m_活动值为“真”,即使在调用stop后也是如此(在stop中设置为false)。m_alive被声明为volatile

找到了m_活着的原因是真的


这是因为,当我们创建一个线程(通常调用“start”函数)时,调用的是另一个线程,它继续执行。当“开始”完成并调用stop时,新线程尚未调度。所以“跑”没有开始。当以后的运行开始时,再次将m_alive的值从false重置为true…

我无法找出问题所在,是否有任何方法可以让join在等待一定时间后退出?使用实际的同步对象(受互斥保护的关键部分、信号量等)而不是简单的易失性变量。