title: How can I tell if there is new data on a pipe?
body: <p>I'm working on Windows, and I'm trying to learn pipes, and how they work.</p>
  
  <p>One thing I haven't found is how can I tell if there is new data on a pipe (from the child/receiver end of the pipe?</p>
  
  <p>The usual method is to have a thread which reads the data, and sends it to be processed:</p>
  
  <pre><code>void GetDataThread()
  {
      while(notDone)
      {
          BOOL result = ReadFile (pipe_handle, buffer, buffer_size, &amp;bytes_read, NULL);
          if (result) DoSomethingWithTheData(buffer, bytes_read);
          else Fail();
      }
  }
  </code></pre>
  
  <p>The problem is that the ReadFile() function waits for data, and then it reads it. Is there a method of telling if there is new data, without actually waiting for new data, like this:</p>
  
  <pre><code>void GetDataThread()
  {
      while(notDone)
      {
          BOOL result = IsThereNewData (pipe_handle);
          if (result) {
               result = ReadFile (pipe_handle, buffer, buffer_size, &amp;bytes_read, NULL);
               if (result) DoSomethingWithTheData(buffer, bytes_read);
               else Fail();
          }
  
          DoSomethingInterestingInsteadOfHangingTheThreadSinceWeHaveLimitedNumberOfThreads();
      }
  }
  </code></pre>