Jump to content
  • Advertisement
Sign in to follow this  
Dookie

Is _endthread() necessary?

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Yup yup. Just to draw analogy, what you did is equivelent to

void foo()
{
cout << "hello" << endl;
return;
cout << "goodbye" << endl;
}

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!