Pipe data to thread. Read stuck

user877329

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:

  1. Close write end of pipe (I expected this to trigger a return from blocking read, but apparently it doesn't)
  2. Wait for reader thread to exit
  3. Restore old file descriptor context (If stdout was redirected to the pipe, it no longer is)
  4. Close read end of pipe
user877329

I found the problem. For redirection, the following has to be done

  1. Create a pipe. This creates two file descriptors. One for reading, and one for writing.
  2. 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 one

Thus, before synchronizing, I have to restore the context. This means that the following order is correct:

  1. Close write end of pipe
  2. Restore old file descriptor context
  3. Wait for reader thread to exit
  4. Close read end of pipe

For reference to the question, step 2 and 3 must be reorder in order to avoid deadlock.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related