I'm looking for a way to interrupt an accept()
call on a blocking socket. Using signals is not an option, as this is meant to be in a library and I don't want to clutter the user signals. Using select()
is another option, buf for various reason it's not very appealing in my case.
What would work well, if possible, is to set the socket to non-blocking mode (using fcntl()
and O_NONBLOCK
) from another thread, while the socket is blocked on an accept()
call. The expected behaviour is that the accept()
call will return with EAGAIN
or EWOULDBLOCK
in errno
.
Would it indeed work like that? Is it safe? Portable?
If you know about the applicability of this method to Windows (where you need to use WSAIoctl()
and FONBIO
), I'm also interested.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…