I have a socket listener which hangs on recv function:
size_t recvLen = recv(sock, buf, 512, 0);
I would like to terminate this thread with interrupting it. MSDN says:
When issuing a blocking Winsock call
such as recv, Winsock may need to wait
for a network event before the call
can complete. Winsock performs an
alertable wait in this situation,
which can be interrupted by an
asynchronous procedure call (APC)
scheduled on the same thread.
How can I do that?
You can interrupt it by queuing an APC to it via
QueueUserAPC. However, it’s most likely unsafe to terminate the thread in the APC. Queuing an APC doesn’t end therecv, it just interrupts it; once the APC returns, it will go back to waiting onrecvagain.If you want to stop the
recvcompletely, you should be usingselectwith a timeout to wait until data is available. You can then check whether you should keep waiting for data or continue at each timeout.