send after shutdown socket

During network programming if a thread is waiting in send or recv on a no longer functioning socket, I can end this by calling shutdown(sock,SHUT_RDWR) from another thread. send or recv will return and the thread itself can close the socket.

But if shutdown(sock,SHUT_RDWR) is called before the send command is called, the program will be killed in response to this send command. Without complicated and slow inter-thread communication this will happen occasionally.

How can I get a thread out of send or recv without this problem?