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

## 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)
{
Sleep(500);
}
...
_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 on other sites
Quote:
 Original post by DookieHello!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:

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

Quote:

##### Share on other sites

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 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 on other sites
Shit! That didn't work! Why not?

##### 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 on other sites
Quote:
 Original post by DookieIf 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...

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 9
• 13
• 19
• 14
• 9
• ### Forum Statistics

• Total Topics
632940
• Total Posts
3009329
• ### Who's Online (See full list)

There are no registered users currently online

×