Nils Pipenbrinck
Nils Pipenbrinck

Reputation: 86363

aborting a blocking read on linux

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

During program shutdown I want to break the thread out of this read. Unfortunately I can't use poll or select and write proper code because the file that is read from is a device driver that does not implement poll/select functionality.

As a temporary solution I currently send a SIGUSR1 signal via pthread_kill to the thread and call pthread_exit from the handler. This kills the thread and works so far, but I'm not satisfied with the solution because the signal could occur anywhere, not only within the read.

Is there any mechanism to interrupt a blocking read on linux?

Btw - I tried closing the file-handle from a different thread in hope that this would give a IO-error of some sorts. Unfortunately this simple solution didn't worked at all.

Upvotes: 4

Views: 2440

Answers (1)

mark4o
mark4o

Reputation: 60923

If you only want the signal to affect the read, use pthread_sigmask() to keep the signal blocked until just before the read and to block it again afterwards.

Upvotes: 5

Related Questions