When a thread is created, a handle is returned which should be closed when its not used anymore.

Can that handle be closed from within the thread function?

My C++ thread class requires this to be possible, and I also think that it is legal and can be done.



Code:
DWORD WINAPI Petter::Thread::threadProc( void* lpData )
{
	//Get the startup data for this thread
	ThreadStartupData* threadData = reinterpret_cast<ThreadStartupData*>( lpData );

	//Obtain a pointer to the thread
	Thread* thread = threadData->thisPtr;

	//Save the handle in the thread object
	thread->threadHandle = threadData->threadHandle;

	//Run the thread's main function
	thread->run();

	//Close the thread's handle
	//WARNING: Can this be done inside the thread?
	CloseHandle(threadData->threadHandle);


	//Delete the thread object
	delete thread;
	delete threadData;

	return 0;
}