title: Block a task until a queue is not empty in Rust
body: <p>I need to dispatch jobs between a set of tasks. The <a href="http://doc.rust-lang.org/std/sync/deque/" rel="nofollow">std::sync::deque</a> is enough to resolve this problem, but I need to block the task if the queue is empty.</p>
  
  <p>The following code (available in a <a href="https://gist.github.com/ayosec/aee066d5e5a3f38c66b9#file-jobs-rs" rel="nofollow">GitHub gist</a>) is a working example of how to use <code>std::sync::deque</code>:</p>
  
  <pre><code>extern crate time;
  
  use std::io::timer::sleep;
  use std::sync::deque::{BufferPool, Empty, Abort, Data};
  use std::time::Duration;
  
  fn main() {
  
    let start = time::precise_time_s();
    let pool = BufferPool::new();
    let (worker, stealer) = pool.deque();
  
    for task_id in range(1i, 5) {
      let sc = stealer.clone();
      spawn(proc() {
        loop {
          let elapse = time::precise_time_s() - start;
          match sc.steal() {
            Empty      =&gt; { println!("[{} @ {:#7.4}] No items", task_id, elapse); sleep(Duration::milliseconds(300)) },
            Abort      =&gt;   println!("[{} @ {:#7.4}] ABORT. Retrying.", task_id, elapse),
            Data(item) =&gt;   println!("[{} @ {:#7.4}] Found {}", task_id, elapse, item)
          }
        }
      });
    }
  
    for item in range(1i, 1000) {
      for n in range(1i, 20) {
        worker.push(item * n);
      }
      sleep(Duration::milliseconds(1000));
    }
  
  }
  </code></pre>
  
  <p>I saw that there is a <a href="http://doc.rust-lang.org/std/sync/struct.TaskPool.html" rel="nofollow">std::sync::TaskPool</a>, but the <a href="https://github.com/rust-lang/rust/blob/master/src/libstd/sync/task_pool.rs#L81-L85" rel="nofollow">current implementation</a> send the job to a task even if the thread is busy with an older job.</p>
  
  <p>My question is: What is the best way to block the task until there is any item in the queue?</p>