3 ms·
> How does one interrupt select() function? Apparently, we used QueueUserAPC() to execute asynchronous procedure while select() was blocked… and threw an except
by oshiar53-0 5y ago
> How does one interrupt select() function? Apparently, we used QueueUserAPC() to execute asynchronous procedure while select() was blocked… and threw an exception out of it!
<rant>
Let's just ignore not using overlapped I/O and completion ports for a moment. How has using APCs even come as a good idea? Like, perhaps a dedicated thread pool for async jobs would work a little better?
If you need to interrupt that specific thread though, sorry for that. Perhaps uhh CancelSynchronousIo() would work since there's IO_STATUS_BLOCK? Or maybe call CancelIo() on the socket in that same APC. However it goes, throwing exceptions in an APC isn't something I'd like to see in production code.
Also, even in Unix world, the select(2) system call is infamously known as a buffer overflow landmine [1]. Just FD_SET any fd >= FD_SETSIZE and BOOM. If you're on Unix, at least use poll() or better. If you're on Windows (and can't afford switching to proper async I/O), maybe WSAEventSelect() would work better?
</rant>
[1]: This does not apply to Windows Sockets though, where fd_set is not a bit array but internally just an FD array in disguise. You get FD_SETSIZE = 64 though, which is conspiciously close to MAXIMUM_WAIT_OBJECTS in WaitForMultupleObjects.