ui中点某一个按钮设置全局变量end
threadfun()
{while(!end)
{
}}
然后在ui中用waitfor 发现根本线程根本没结束,ui被阻塞.是不是和线程的优先级有关?

解决方案 »

  1.   

    首先在VC中暂停程序,看看线程是没有结束,还是结束了waitfor没有反应。如果是前者,有可能由于优化有while(!end)时没有每次读取end的值,在end的声明前加上volatile,这个关键字就是这时候用的。  The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread。可以参看MSDN。如果是后者,可能是没有设置CWinThread::m_bAutoDelete,可以参看http://community.csdn.net/Expert/TopicView.asp?id=4676405