|
23 | 23 | //! |
24 | 24 | //! These channels come in two flavors: |
25 | 25 | //! |
26 | | -//! 1. An asynchronous, infinitely buffered channel. The [`channel()`] function |
| 26 | +//! 1. An asynchronous, infinitely buffered channel. The [`channel`] function |
27 | 27 | //! will return a `(Sender, Receiver)` tuple where all sends will be |
28 | 28 | //! **asynchronous** (they never block). The channel conceptually has an |
29 | 29 | //! infinite buffer. |
30 | 30 | //! |
31 | | -//! 2. A synchronous, bounded channel. The [`sync_channel()`] function will |
| 31 | +//! 2. A synchronous, bounded channel. The [`sync_channel`] function will |
32 | 32 | //! return a `(SyncSender, Receiver)` tuple where the storage for pending |
33 | 33 | //! messages is a pre-allocated buffer of a fixed size. All sends will be |
34 | 34 | //! **synchronous** by blocking until there is buffer space available. Note |
|
39 | 39 | //! [`SyncSender`]: ../../../std/sync/mpsc/struct.SyncSender.html |
40 | 40 | //! [`Receiver`]: ../../../std/sync/mpsc/struct.Receiver.html |
41 | 41 | //! [`send`]: ../../../std/sync/mpsc/struct.Sender.html#method.send |
42 | | -//! [`channel()`]: ../../../std/sync/mpsc/fn.channel.html |
43 | | -//! [`sync_channel()`]: ../../../std/sync/mpsc/fn.sync_channel.html |
| 42 | +//! [`channel`]: ../../../std/sync/mpsc/fn.channel.html |
| 43 | +//! [`sync_channel`]: ../../../std/sync/mpsc/fn.sync_channel.html |
44 | 44 | //! |
45 | 45 | //! ## Disconnection |
46 | 46 | //! |
|
51 | 51 | //! |
52 | 52 | //! Once half of a channel has been deallocated, most operations can no longer |
53 | 53 | //! continue to make progress, so [`Err`] will be returned. Many applications |
54 | | -//! will continue to [`unwrap()`] the results returned from this module, |
| 54 | +//! will continue to [`unwrap`] the results returned from this module, |
55 | 55 | //! instigating a propagation of failure among threads if one unexpectedly dies. |
56 | 56 | //! |
57 | 57 | //! [`Result`]: ../../../std/result/enum.Result.html |
58 | 58 | //! [`Err`]: ../../../std/result/enum.Result.html#variant.Err |
59 | | -//! [`unwrap()`]: ../../../std/result/enum.Result.html#method.unwrap |
| 59 | +//! [`unwrap`]: ../../../std/result/enum.Result.html#method.unwrap |
60 | 60 | //! |
61 | 61 | //! # Examples |
62 | 62 | //! |
@@ -310,12 +310,15 @@ mod spsc_queue; |
310 | 310 | /// use std::sync::mpsc::channel; |
311 | 311 | /// use std::thread; |
312 | 312 | /// use std::time::Duration; |
| 313 | +/// |
313 | 314 | /// let (send, recv) = channel(); |
| 315 | +/// |
314 | 316 | /// thread::spawn(move || { |
315 | 317 | /// send.send("Hello world!").unwrap(); |
316 | 318 | /// thread::sleep(Duration::from_secs(2)); // block for two seconds |
317 | 319 | /// send.send("Delayed for 2 seconds").unwrap(); |
318 | 320 | /// }); |
| 321 | +/// |
319 | 322 | /// println!("{}", recv.recv().unwrap()); // Received immediately |
320 | 323 | /// println!("Waiting..."); |
321 | 324 | /// println!("{}", recv.recv().unwrap()); // Received after 2 seconds |
@@ -384,18 +387,23 @@ pub struct IntoIter<T> { |
384 | 387 | /// ```rust |
385 | 388 | /// use std::sync::mpsc::channel; |
386 | 389 | /// use std::thread; |
| 390 | +/// |
387 | 391 | /// let (sender, receiver) = channel(); |
388 | 392 | /// let sender2 = sender.clone(); |
| 393 | +/// |
389 | 394 | /// // First thread owns sender |
390 | 395 | /// thread::spawn(move || { |
391 | 396 | /// sender.send(1).unwrap(); |
392 | 397 | /// }); |
| 398 | +/// |
393 | 399 | /// // Second thread owns sender2 |
394 | 400 | /// thread::spawn(move || { |
395 | 401 | /// sender2.send(2).unwrap(); |
396 | 402 | /// }); |
| 403 | +/// |
397 | 404 | /// let msg = receiver.recv().unwrap(); |
398 | 405 | /// let msg2 = receiver.recv().unwrap(); |
| 406 | +/// |
399 | 407 | /// assert_eq!(3, msg + msg2); |
400 | 408 | /// ``` |
401 | 409 | #[stable(feature = "rust1", since = "1.0.0")] |
@@ -1097,12 +1105,15 @@ impl<T> Receiver<T> { |
1097 | 1105 | /// ```rust |
1098 | 1106 | /// use std::sync::mpsc::channel; |
1099 | 1107 | /// use std::thread; |
| 1108 | + /// |
1100 | 1109 | /// let (send, recv) = channel(); |
| 1110 | + /// |
1101 | 1111 | /// thread::spawn(move || { |
1102 | 1112 | /// send.send(1u8).unwrap(); |
1103 | 1113 | /// send.send(2u8).unwrap(); |
1104 | 1114 | /// send.send(3u8).unwrap(); |
1105 | 1115 | /// }); |
| 1116 | + /// |
1106 | 1117 | /// for x in recv.iter() { |
1107 | 1118 | /// println!("Got: {}", x); |
1108 | 1119 | /// } |
|
0 commit comments