Sign in to follow this  
dev578

Ending Threads

Recommended Posts

dev578    128
I have a thread that is created by CreateThread(). I am using it to accept network connections, right now the socket is a blocking socket, so accept waits for connections. Usually when I want to end threads, I set a bool and the thread returns zero from within itself, to my understanding this is the preferred way of doing so? Obviously in this thread I am going to run into problems because of accept. I can make it non-blocking and loop the crap out of it, it just seems illogical to make that many accept calls. I also thought of using TerminateThread, but MSDN seems to think otherwise: The TerminateThread and TerminateProcess functions should be used only in extreme circumstances. That made me laugh:) They suggest getting around this by WaitForSingleObject(), which is also a function and can't be called because it waits at accept. I'm not sure what to do. Is there something I'm missing? Thank you in advance Dev578

Share this post


Link to post
Share on other sites
Telastyn    3777
Personally, I would use select with the listening connection, with a sane timeout (~30 seconds?). It'd look akin to:

while(!quit && -1!=select(...,InSet,...,...,Timeout)){
if(ListeningSocket is in InSet){
accept(...);
// ...
}
// ...
}





This would effectively accept as needed, and check every 30 seconds or so if the quit bool is set. select is a little weird to work with, but there's a number of threads in the Networking forum that can help.

Share this post


Link to post
Share on other sites
LessBread    1415
Quote:
Original post by dev578
I have a thread that is created by CreateThread(). I am using it to accept network connections, right now the socket is a blocking socket, so accept waits for connections. Usually when I want to end threads, I set a bool and the thread returns zero from within itself, to my understanding this is the preferred way of doing so? Obviously in this thread I am going to run into problems because of accept. I can make it non-blocking and loop the crap out of it, it just seems illogical to make that many accept calls. I also thought of using TerminateThread, but MSDN seems to think otherwise:

The TerminateThread and TerminateProcess functions should be used only in extreme circumstances.

That made me laugh:) They suggest getting around this by WaitForSingleObject(), which is also a function and can't be called because it waits at accept. I'm not sure what to do. Is there something I'm missing?

Thank you in advance

Dev578


Have you looked into ExitThread?



Share this post


Link to post
Share on other sites
dev578    128
Good idea, that is what I will do. Thank you.

Dev578

Edit: ExitThread() would have to be called from within the thread right? That is the problem I was having. select() seems like it will do the trick, although It might be a slight annoyance having to wait a potential maximum of 30/x seconds for the thread to end.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this