Skip to content

Commit 5f05342

Browse files
committed
---
yaml --- r: 152553 b: refs/heads/try2 c: 0973eb4 h: refs/heads/master i: 152551: 50ac49b v: v3
1 parent 263c946 commit 5f05342

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
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 2fe926431bb198a052a5eae92ff820a4f572fd92
8+
refs/heads/try2: 0973eb4419d0598c1134106adef2ee8dc2a2b5ff
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

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