I want to trigger a callback when data is written on a file descriptor. For this I have set up a pipe and a reader thread, which reads the pipe. When it has data, the callback is called with the data.
The problem is that the reader is stuck on the read
syscall. Destruction order is as follows:
read
, but apparently it doesn't)I found the problem. For redirection, the following has to be done
dup2
so the original file descriptor is an alias to the write end of the pipe. This increments the use count of the write end by oneThus, before synchronizing, I have to restore the context. This means that the following order is correct:
For reference to the question, step 2 and 3 must be reorder in order to avoid deadlock.