Linux – Aborts blocking reads on Linux

Aborts blocking reads on Linux… here is a solution to the problem.

Aborts blocking reads on Linux

I have a blocking read in linux running in a thread.

During program shutdown, I want to disconnect the thread from the read. Unfortunately, I can’t use polling or select and write the correct code because the file read from it is a device driver that doesn’t implement polling/selecting functionality.

As a temporary solution, I currently send a SIGUSR1 signal to the thread via pthread_kill and call pthread_exit from the handler. This terminates the thread and works so far, but I’m not happy with the solution because the signal can happen anywhere, not just in the read.

Is there any mechanism to interrupt blocking reads on linux?

BTW – I tried closing the file handle from another thread, hoping this would produce some kind of IO error. Unfortunately, this simple solution doesn’t work at all.


If you only want the signal to affect the read, use pthread_ sigmask() keeps the signal blocked until it is read, and then blocks again.

Related Problems and Solutions