Suppose i have pointer to a thread like this
CWinThread *m_pThread = AfxBeginThread(StartThread, this, THREAD_PRIORITY_NORMAL, 0, 0);
Now in my StartThread function assume i did all operations and the function returned like this
UINT CClassThread::StartThread(LPVOID pVoid)
{
return true;
}
Will my m_pThread be invalid when the return statement is executed?
Yes, it will be invalid because it will be deleted at the end of the thread. However, if you set
CWinThread::m_bAutoDeletetoFALSE, theCWinThread*won’t be deleted. I just googled for the answer and found it here: http://msdn.microsoft.com/en-us/library/48xz4yz9(VS.80).aspxAs an aside, if you were using pthreads (Unix threads), the answer would be no. When a thread terminates, its handle remains valid until you call
pthread_join(wait for a thread to finish, deallocate it, and get its return value) orpthread_detach(tell a thread to deallocate itself when it completes).