views:

78

answers:

1

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?

+5  A: 

Yes, it will be invalid because it will be deleted at the end of the thread. However, if you set CWinThread::m_bAutoDelete to FALSE, the CWinThread* won't be deleted. I just googled for the answer and found it here: http://msdn.microsoft.com/en-us/library/48xz4yz9(VS.80).aspx

As 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) or pthread_detach (tell a thread to deallocate itself when it completes).

Joey Adams
Ok and by being invalid would it be NULL or some data which is not valid.
ckv
It would be a wild pointer (invalid). C++ won't hunt down your pointers and NULL them out when they become invalid :-)
Joey Adams
`m_pThread` will keep it's value, even though the object it pointed to is deallocated. It will become a [dangling pointer](http://en.wikipedia.org/wiki/Dangling_pointer).
Anders Abel