Skip to content

Commit 2f25a15

Browse files
committed
---
yaml --- r: 117820 b: refs/heads/auto c: 0973eb4 h: refs/heads/master v: v3
1 parent 361b3ab commit 2f25a15

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1313
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1414
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1515
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
16-
refs/heads/auto: 2fe926431bb198a052a5eae92ff820a4f572fd92
16+
refs/heads/auto: 0973eb4419d0598c1134106adef2ee8dc2a2b5ff
1717
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1818
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1919
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/libsync/comm/mod.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,52 @@
120120
//! });
121121
//! rx.recv();
122122
//! ```
123+
//!
124+
//! Reading from a channel with a timeout requires to use a Timer together
125+
//! with the channel. You can use the select! macro to select either and
126+
//! handle the timeout case. This first example will break out of the loop
127+
//! after 10 seconds no matter what:
128+
//!
129+
//! ```no_run
130+
//! use std::io::timer::Timer;
131+
//!
132+
//! let (tx, rx) = channel::<int>();
133+
//! let mut timer = Timer::new().unwrap();
134+
//! let timeout = timer.oneshot(10000);
135+
//!
136+
//! loop {
137+
//! select! {
138+
//! val = rx.recv() => println!("Received {}", val),
139+
//! () = timeout.recv() => {
140+
//! println!("timed out, total time was more than 10 seconds")
141+
//! break;
142+
//! }
143+
//! }
144+
//! }
145+
//! ```
146+
//!
147+
//! This second example is more costly since it allocates a new timer every
148+
//! time a message is received, but it allows you to timeout after the channel
149+
//! has been inactive for 5 seconds:
150+
//!
151+
//! ```no_run
152+
//! use std::io::timer::Timer;
153+
//!
154+
//! let (tx, rx) = channel::<int>();
155+
//! let mut timer = Timer::new().unwrap();
156+
//!
157+
//! loop {
158+
//! let timeout = timer.oneshot(5000);
159+
//!
160+
//! select! {
161+
//! val = rx.recv() => println!("Received {}", val),
162+
//! () = timeout.recv() => {
163+
//! println!("timed out, no message received in 5 seconds")
164+
//! break;
165+
//! }
166+
//! }
167+
//! }
168+
//! ```
123169
124170
// A description of how Rust's channel implementation works
125171
//

0 commit comments

Comments
 (0)