title: Opening 2 pipes in c++ from 1 program
body: <p>I have a program that I am writing for an embedded device, and I am trying to use pipes to pass messages.  Before I get to passing messages between my program and another, I was building test code to ensure that everything is working properly, and encountered a problem.  Note that the embedded device doesn't support c++11 (hence the use of pthread's).</p>
  
  <p>The code in question:</p>
  
  <pre><code>void* testSender(void *ptr) {
    std::cout &lt;&lt; "Beginning testSender" &lt;&lt; std::endl;
    int pipe = open("/dev/rtp10", O_WRONLY);
    if (pipe &lt; 0) {
      std::cout &lt;&lt; "write pipe failed to open" &lt;&lt; std::endl;
    } else {
      std::cout &lt;&lt; "write pipe successfully opened" &lt;&lt; std::endl;
    }
    std::cout &lt;&lt; "Ending testSender" &lt;&lt; std::endl;
    return NULL;
  }
  
  void* testReceiver(void *ptr) {
    std::cout &lt;&lt; "Beginning testReceiver" &lt;&lt; std::endl;
    int pipe = open("/dev/rtp10", O_RDONLY);
    if (pipe &lt; 0) {
      std::cout &lt;&lt; "read pipe failed to open" &lt;&lt; std::endl;
    } else {
      std::cout &lt;&lt; "read pipe successfully opened" &lt;&lt; std::endl;
    }
    std::cout &lt;&lt; "Ending testReceiver" &lt;&lt; std::endl;
    return NULL;
  }
  
  void testOpenClosePipes() {
    std::cout &lt;&lt; "Beginning send/receive test" &lt;&lt; std::endl;
    pthread_t sendThread, receiveThread;
    pthread_create(&amp;sendThread, NULL, &amp;testSender, NULL);
    pthread_create(&amp;receiveThread, NULL, &amp;testReceiver, NULL);
    std::cout &lt;&lt; "waiting for send and receive test" &lt;&lt; std::endl;
    pthread_join(receiveThread, NULL);
    pthread_join(sendThread, NULL);
    std::cout &lt;&lt; "Done testing open send/receive" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>The function testOpenClosePipes() is called from my main thread and after calling it I get the following output:</p>
  
  <pre><code>Beginning send/receive test
  waiting for send and receive test
  Beginning testReceiver
  Beginning testSender
  write pipe failed to open
  Ending testSender
  </code></pre>
  
  <p>and then the program hangs.  I believe that this is because the read pipe has been opened and is then waiting for a sender to connect to the pipe, but I could be wrong there.  Note that if I start the receive thread before I start the send thread, then the result is as follows:</p>
  
  <pre><code>Beginning send/receive test
  waiting for send and receive test
  Beginning testSender
  Beginning testReceiver
  read pipe failed to open
  Ending testReceiver
  </code></pre>
  
  <p>From what I have read about pipes so far, what appears to be occurring is that one of the two (either send or receive) is opening correctly, and then holding until the other end of the pipe has been opened.  However, the other end of the pipe is failing to open correctly, which ends up leaving the system hanging because the open pipe is waiting for its connection before it successfully moves on.  I am unable to figure out why this is happening however, and am looking to get help with that.</p>