|
105 | 105 | //! });
|
106 | 106 | //! rx.recv().unwrap();
|
107 | 107 | //! ```
|
| 108 | +//! |
| 109 | +//! Unbounded receive loop: |
| 110 | +//! |
| 111 | +//! ``` |
| 112 | +//! use std::sync::mpsc::sync_channel; |
| 113 | +//! use std::thread; |
| 114 | +//! |
| 115 | +//! let (tx, rx) = sync_channel(3); |
| 116 | +//! |
| 117 | +//! for _ in 0..3 { |
| 118 | +//! // It would be the same without thread and clone here |
| 119 | +//! // since there will still be one `tx` left. |
| 120 | +//! let tx = tx.clone(); |
| 121 | +//! // cloned tx dropped within thread |
| 122 | +//! thread::spawn(move || tx.send("ok").unwrap()); |
| 123 | +//! } |
| 124 | +//! |
| 125 | +//! // Drop the last sender to stop `rx` waiting for message. |
| 126 | +//! // The program will not complete if we comment this out. |
| 127 | +//! // **All** `tx` needs to be dropped for `rx` to have `Err`. |
| 128 | +//! drop(tx); |
| 129 | +//! |
| 130 | +//! // Unbounded receiver waiting for all senders to complete. |
| 131 | +//! while let Ok(msg) = rx.recv() { |
| 132 | +//! println!("{}", msg); |
| 133 | +//! } |
| 134 | +//! |
| 135 | +//! println!("completed"); |
| 136 | +//! ``` |
108 | 137 |
|
109 | 138 | #![stable(feature = "rust1", since = "1.0.0")]
|
110 | 139 |
|
@@ -436,6 +465,9 @@ pub struct IntoIter<T> {
|
436 | 465 | ///
|
437 | 466 | /// Messages can be sent through this channel with [`send`].
|
438 | 467 | ///
|
| 468 | +/// Note: all senders (the original and the clones) need to be dropped for the receiver |
| 469 | +/// to stop blocking to receive messages with [`Receiver::recv`]. |
| 470 | +/// |
439 | 471 | /// [`send`]: Sender::send
|
440 | 472 | ///
|
441 | 473 | /// # Examples
|
@@ -642,7 +674,7 @@ impl<T> UnsafeFlavor<T> for Receiver<T> {
|
642 | 674 | /// the same order as it was sent, and no [`send`] will block the calling thread
|
643 | 675 | /// (this channel has an "infinite buffer", unlike [`sync_channel`], which will
|
644 | 676 | /// block after its buffer limit is reached). [`recv`] will block until a message
|
645 |
| -/// is available. |
| 677 | +/// is available while there is at least one [`Sender`] alive (including clones). |
646 | 678 | ///
|
647 | 679 | /// The [`Sender`] can be cloned to [`send`] to the same channel multiple times, but
|
648 | 680 | /// only one [`Receiver`] is supported.
|
@@ -805,6 +837,11 @@ impl<T> Sender<T> {
|
805 | 837 |
|
806 | 838 | #[stable(feature = "rust1", since = "1.0.0")]
|
807 | 839 | impl<T> Clone for Sender<T> {
|
| 840 | + /// Clone a sender to send to other threads. |
| 841 | + /// |
| 842 | + /// Note, be aware of the lifetime of the sender because all senders |
| 843 | + /// (including the original) need to be dropped in order for |
| 844 | + /// [`Receiver::recv`] to stop blocking. |
808 | 845 | fn clone(&self) -> Sender<T> {
|
809 | 846 | let packet = match *unsafe { self.inner() } {
|
810 | 847 | Flavor::Oneshot(ref p) => {
|
@@ -1063,9 +1100,10 @@ impl<T> Receiver<T> {
|
1063 | 1100 | /// corresponding channel has hung up.
|
1064 | 1101 | ///
|
1065 | 1102 | /// This function will always block the current thread if there is no data
|
1066 |
| - /// available and it's possible for more data to be sent. Once a message is |
1067 |
| - /// sent to the corresponding [`Sender`] (or [`SyncSender`]), then this |
1068 |
| - /// receiver will wake up and return that message. |
| 1103 | + /// available and it's possible for more data to be sent (at least one sender |
| 1104 | + /// still exists). Once a message is sent to the corresponding [`Sender`] |
| 1105 | + /// (or [`SyncSender`]), this receiver will wake up and return that |
| 1106 | + /// message. |
1069 | 1107 | ///
|
1070 | 1108 | /// If the corresponding [`Sender`] has disconnected, or it disconnects while
|
1071 | 1109 | /// this call is blocking, this call will wake up and return [`Err`] to
|
@@ -1145,9 +1183,10 @@ impl<T> Receiver<T> {
|
1145 | 1183 | /// corresponding channel has hung up, or if it waits more than `timeout`.
|
1146 | 1184 | ///
|
1147 | 1185 | /// This function will always block the current thread if there is no data
|
1148 |
| - /// available and it's possible for more data to be sent. Once a message is |
1149 |
| - /// sent to the corresponding [`Sender`] (or [`SyncSender`]), then this |
1150 |
| - /// receiver will wake up and return that message. |
| 1186 | + /// available and it's possible for more data to be sent (at least one sender |
| 1187 | + /// still exists). Once a message is sent to the corresponding [`Sender`] |
| 1188 | + /// (or [`SyncSender`]), this receiver will wake up and return that |
| 1189 | + /// message. |
1151 | 1190 | ///
|
1152 | 1191 | /// If the corresponding [`Sender`] has disconnected, or it disconnects while
|
1153 | 1192 | /// this call is blocking, this call will wake up and return [`Err`] to
|
|
0 commit comments