首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

多线程 m_bAutoDelete和ResumeThread如何理解

2013-07-04 
多线程 m_bAutoDelete和ResumeThread怎么理解?看到这么一段代码m_pThread AfxBeginThread(LoadFunction,

多线程 m_bAutoDelete和ResumeThread怎么理解?
看到这么一段代码



m_pThread = AfxBeginThread(LoadFunction, this, THREAD_PRIORITY_LOWEST, 0, CREATE_SUSPENDED);
m_pThread ->m_bAutoDelete = TRUE;
m_pThread ->ResumeThread();


1 我知道m_bAutoDelete = true的意思是自动删除 想知道自动删除是在什么时候删除?是操作系统认为合适的时候吗?
2 ResumeThread我也查了查意思,是不是说在合适的情况下如果这个线程还在挂起,就激活继续运行?

多线程刚开始接触 都是菜鸟问题 - -b
有例子最好了
谢谢了
[解决办法]

CWinThread* AFXAPI AfxBeginThread(CRuntimeClass* pThreadClass,
int nPriority, UINT nStackSize, DWORD dwCreateFlags,
LPSECURITY_ATTRIBUTES lpSecurityAttrs)
{
#ifndef _MT
pThreadClass;
nPriority;
nStackSize;
dwCreateFlags;
lpSecurityAttrs;

return NULL;
#else
ASSERT(pThreadClass != NULL);
ASSERT(pThreadClass->IsDerivedFrom(RUNTIME_CLASS(CWinThread)));

CWinThread* pThread = (CWinThread*)pThreadClass->CreateObject();
if (pThread == NULL)
AfxThrowMemoryException();
ASSERT_VALID(pThread);

pThread->m_pThreadParams = NULL;
if (!pThread->CreateThread(dwCreateFlags
[解决办法]
CREATE_SUSPENDED, nStackSize,
lpSecurityAttrs))
{
pThread->Delete();
return NULL;
}
VERIFY(pThread->SetThreadPriority(nPriority));
if (!(dwCreateFlags & CREATE_SUSPENDED))
{
ENSURE(pThread->ResumeThread() != (DWORD)-1);
}

return pThread;
#endif //!_MT
}



void CWinThread::Delete()
{
// delete thread if it is auto-deleting
if (m_bAutoDelete)
delete this;
}

创建线程失败的时候,线程结束的时候,打个断点调试下就知道了。
线程创建的时候指定了CREATE_SUSPENDED,创建时就是挂起状态,然后再ResumeThread恢复,这种做法一般是用来在创建线程后,resumethread之前,让线程类做一些初始化的工作。

热点排行