Is _endthread() necessary?

Started by
6 comments, last by the_edd 15 years, 4 months ago
Hello! I have some code called with '_beginthread()' and it executes fine. But when I get to the end of the thread function, I call '_endthread()' and it doesn't go beyond that... For example (pseudocode):
void MyThread(void *Duh)
{
	print "Thread entered successfully."
	Sleep(500);
	print "Gonna execute '_endthread()'..."
	_endthread();
	print "Thread ended successfully"
}
...
_beginthread(MyThread, 0, 0);
will display:
Thread entered successfully.
Gonna execute '_endthread()'...
It never displays that last part ("Thread ended successfully") because it gets hung up on the _endthread() statement. Am I doing something wrong? Are resources getting hogged up because of this? Is _endthread() even necessary? Am I nuts? Thanks in advance for the help!
"The crows seemed to be calling his name, thought Caw"
Advertisement
Quote:Original post by Dookie
Hello!

I have some code called with '_beginthread()' and it executes fine. But when I get to the end of the thread function, I call '_endthread()' and it doesn't go beyond that...


Of course not! You've ended the thread.


Quote:Is _endthread() even necessary?


Have you read the MSDN page? It's pretty clear, I think. The relevant quote:

Quote:You can call _endthread or _endthreadex explicitly to terminate a thread; however, _endthread or _endthreadex is called automatically when the thread returns from the routine passed as a parameter to _beginthread or _beginthreadex. Terminating a thread with a call to endthread or _endthreadex helps ensure proper recovery of resources allocated for the thread.
Thanks for the reply, the_edd!

If you look at the code segment, you'll notice that "_endthread()" was being called from within the MyThread() function itself (the function called by "_beginthread()"). The thread had not ended yet, so I was going to use _endthread() at the end of the function to formally free up any resources it may have been using.

I have indeed read the MSDN page on threads, and I've heard differing opinions here and there whether it's necessary to manually shut down a thread. I figured that I probably didn't need to actually use _endthread(), but I was just baffled as to why the function was getting stuck when it tried to execute _endthread().

I still don't know why the MyThread() function stops at _endthread()... Anybody else know why?

Thanks in advance for the help!
"The crows seemed to be calling his name, thought Caw"
Because _endthread() ends the thread.
I'm going to type the word DUCK, and then I'm going to hit "Reply", and then I'm going to type the word SOUP.

DUCK
Shit! That didn't work! Why not?
Yup yup. Just to draw analogy, what you did is equivelent to

void foo()
{
cout << "hello" << endl;
return;
cout << "goodbye" << endl;
}
Quote:Original post by Dookie
If you look at the code segment, you'll notice that "_endthread()" was being called from within the MyThread() function itself (the function called by "_beginthread()").


Yes, that is the thread that _endthread() acts upon (the one in which it is called).

Indeed, given _endthread's prototype, it simply cannot act on another thread, because how would it know which other thread to terminate? You don't pass it a thread identifier of any kind and there can be more than two threads in an app...

This topic is closed to new replies.

Advertisement