diff options
| author | bors <bors@rust-lang.org> | 2014-06-16 17:01:56 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-06-16 17:01:56 +0000 |
| commit | 4b672a63da3083ab6d4673cdb223bc1c978ed7d2 (patch) | |
| tree | 0bad01e7f25a06cf5e97481c1dfbc1b9159218f2 | |
| parent | 0b32d42a5da84c1f23a2b50b9a6741eea69773c4 (diff) | |
| parent | 5412fdacaf273bf3ce3230a078ff8ad156cc61a2 (diff) | |
| download | rust-4b672a63da3083ab6d4673cdb223bc1c978ed7d2.tar.gz rust-4b672a63da3083ab6d4673cdb223bc1c978ed7d2.zip | |
auto merge of #14877 : Seldaek/rust/commdocs, r=alexcrichton
Finally what I promised to do in #13862 /cc @alexcrichton
| -rw-r--r-- | src/libsync/comm/mod.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/libsync/comm/mod.rs b/src/libsync/comm/mod.rs index a7523eff04f..842743657a6 100644 --- a/src/libsync/comm/mod.rs +++ b/src/libsync/comm/mod.rs @@ -120,6 +120,52 @@ //! }); //! rx.recv(); //! ``` +//! +//! Reading from a channel with a timeout requires to use a Timer together +//! with the channel. You can use the select! macro to select either and +//! handle the timeout case. This first example will break out of the loop +//! after 10 seconds no matter what: +//! +//! ```no_run +//! use std::io::timer::Timer; +//! +//! let (tx, rx) = channel::<int>(); +//! let mut timer = Timer::new().unwrap(); +//! let timeout = timer.oneshot(10000); +//! +//! loop { +//! select! { +//! val = rx.recv() => println!("Received {}", val), +//! () = timeout.recv() => { +//! println!("timed out, total time was more than 10 seconds") +//! break; +//! } +//! } +//! } +//! ``` +//! +//! This second example is more costly since it allocates a new timer every +//! time a message is received, but it allows you to timeout after the channel +//! has been inactive for 5 seconds: +//! +//! ```no_run +//! use std::io::timer::Timer; +//! +//! let (tx, rx) = channel::<int>(); +//! let mut timer = Timer::new().unwrap(); +//! +//! loop { +//! let timeout = timer.oneshot(5000); +//! +//! select! { +//! val = rx.recv() => println!("Received {}", val), +//! () = timeout.recv() => { +//! println!("timed out, no message received in 5 seconds") +//! break; +//! } +//! } +//! } +//! ``` // A description of how Rust's channel implementation works // |
