title: CFReadStreamHasBytesAvailable polling - best practices
body: <p>I'm currently polling my <code>CFReadStream</code> for new data with <code>CFReadStreamHasBytesAvailable</code>.</p>
  
  <p>(First, some background: I'm doing my own threading and I don't want/need to mess with runloop stuff, so the client callback stuff doesn't really apply here).</p>
  
  <p>My question is: what are accepted practices for polling?</p>
  
  <p><a href="http://developer.apple.com/documentation/Networking/Conceptual/CFNetwork/CFStreamTasks/chapter_4_section_4.html#//apple_ref/doc/uid/TP30000230-62008" rel="nofollow noreferrer">Apple's documentation</a> on the subject doesn't seem too helpful.</p>
  
  <p>They recommend to "do something else while you wait".  I'm currently just doing something along the lines of:</p>
  
  <pre><code>while(!done)
  {
    if(CFReadStreamHasBytesAvailable(readStream))
    {
      CFReadStreamRead(...) ... bla bla bla
    } else {
      usleep(3600); // I made this up
      sched_yield(); // also made this up
      continue;
    }
  }
  </code></pre>
  
  <p>Is the <code>usleep</code> and the <code>sched_yield</code> "good enough"?  In there a "good" number to sleep for in <code>usleep</code>?</p>
  
  <p>(Also: yes, because this is running in my own thread, I could just block on <code>CFReadStreamRead</code> - which would be great but I'm also trying to snag upload progress as well as download progress, so blocking there wouldn't help...).</p>
  
  <p>Any insight would be much appreciated - thanks!</p>