Skip to content

Commit f982837

Browse files
committed
---
yaml --- r: 118569 b: refs/heads/try c: 0973eb4 h: refs/heads/master i: 118567: 3097e2a v: v3
1 parent 84de79d commit f982837

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
@@ -2,7 +2,7 @@
22
refs/heads/master: b1646cbfd908dc948b251e362669af421100647a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: d6736a1440d42f6af967a8a20ab8d73522112b72
5-
refs/heads/try: 2fe926431bb198a052a5eae92ff820a4f572fd92
5+
refs/heads/try: 0973eb4419d0598c1134106adef2ee8dc2a2b5ff
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/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)