title: How to break from the blocking TcpListener::accept call?
body: <p>I'm running the <a href="http://doc.rust-lang.org/1.0.0-beta/std/net/struct.TcpListener.html#method.accept" rel="noreferrer"><code>accept</code> function of a TCP listener</a> in a loop in a separate thread. I would like to shutdown this thread gracefully, but I can't see any kind of <code>shutdown</code> mechanism which I could use to break from accepting.</p>
  
  <p>My current approach looks something like this:</p>
  
  <pre><code>use std::net::TcpListener;
  use std::thread::spawn;
  
  fn main() {
      let tcp_listener = TcpListener::bind((("0.0.0.0"), 0)).unwrap();
      let tcp_listener2 = tcp_listener.try_clone().unwrap();
  
      let t = spawn(move || {
          loop {
              match tcp_listener2.accept() {
                  Ok(_) =&gt; { }
                  Err(_) =&gt; { break; }
             }
          }
      });
  
      drop(tcp_listener);
      assert!(t.join().is_ok());
  }
  </code></pre>
  
  <p>But that doesn't do the trick (probably because I drop only the cloned copy?). Any thought on how to properly shutdown such thread?</p>
  
  <p>(for reference, <a href="http://users.rust-lang.org/t/how-to-break-from-the-blocking-tcplistener-accept-call/894/1" rel="noreferrer">I asked this question on rust user forum as well</a>)</p>