Terminating the Worker thread
Hi to all.
I have got a senario to kill the worker thread.
my thread procedure is as
UINT ListenThread(LPVOID lp)
{
...
...
while(kill) // when true terminte thread
{
WSAAccept(...);
//tackling some stuff
}
return 1;
}
i want to kill the thread on termination of the program, no matter what job it is doing. problem here is this thread is stuck on WSAAccept() and i found no way out of it unless an incoming connection request.
i have also tried the TerminatThread() function passing it the m_pListenThread got when
CWinThread * m_pListenThread;
m_pListenThread=AfxBeginThread(ListenThread, (LPVOID)port);
but it also did not work.
can any one give me suggestion what to do in this case?
[890 byte] By [
Mahroze] at [2007-11-20 1:26:29]

# 3 Re: Terminating the Worker thread
Keep your original code (i.e., don't use TerminateThread).
To get your thread out of the WSAAccept() function, call closesocket from your main thread, on the listening socket. It's always permissible to call closesocket from another thread. When the socket closes, WSAAccept() will return immediately, and your thread's loop will then be free to check the "kill" variable to exit normally.
Mike
# 6 Re: Terminating the Worker thread
i had studied it and checked TerminatThread() but it did'nt workeed
Probably because the blocking call to WSA** was waiting, TerminateThread dosent kill instantly, it needs some safe state of the thread to terminate it.
# 7 Re: Terminating the Worker thread
Keep your original code (i.e., don't use TerminateThread).
To get your thread out of the WSAAccept() function, call closesocket from your main thread, on the listening socket. It's always permissible to call closesocket from another thread. When the socket closes, WSAAccept() will return immediately, and your thread's loop will then be free to check the "kill" variable to exit normally.
Mike
:thumb: Thats the best aproach.