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]
# 1 Re: Terminating the Worker thread
How to end a thread? ( http://www.dev-archive.com/forum/showthread.php?t=305166)

Cheers
golanshahar at 2007-11-10 23:18:29 >
# 2 Re: Terminating the Worker thread
i had studied it and checked TerminatThread() but it did'nt workeed
Mahroze at 2007-11-10 23:19:35 >
# 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
MikeAThon at 2007-11-10 23:20:34 >
# 4 Re: Terminating the Worker thread
Thx Mike, it worked.
Mahroze at 2007-11-10 23:21:33 >
# 5 Re: Terminating the Worker thread
You're welcome. Glad it worked out for you.

Mike
MikeAThon at 2007-11-10 23:22:32 >
# 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.
Krishnaa at 2007-11-10 23:23:31 >
# 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.
Krishnaa at 2007-11-10 23:24:32 >