|
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 |
|
@@ -437,6 +466,9 @@ pub struct IntoIter<T> {
|
437 | 466 | ///
|
438 | 467 | /// Messages can be sent through this channel with [`send`].
|
439 | 468 | ///
|
| 469 | +/// Note: all senders (the original and the clones) need to be dropped for the receiver |
| 470 | +/// to stop blocking to receive messages with [`Receiver::recv`]. |
| 471 | +/// |
440 | 472 | /// [`send`]: Sender::send
|
441 | 473 | ///
|
442 | 474 | /// # Examples
|
@@ -643,7 +675,7 @@ impl<T> UnsafeFlavor<T> for Receiver<T> {
|
643 | 675 | /// the same order as it was sent, and no [`send`] will block the calling thread
|
644 | 676 | /// (this channel has an "infinite buffer", unlike [`sync_channel`], which will
|
645 | 677 | /// block after its buffer limit is reached). [`recv`] will block until a message
|
646 |
| -/// is available. |
| 678 | +/// is available while there is at least one [`Sender`] alive (including clones). |
647 | 679 | ///
|
648 | 680 | /// The [`Sender`] can be cloned to [`send`] to the same channel multiple times, but
|
649 | 681 | /// only one [`Receiver`] is supported.
|
@@ -806,6 +838,11 @@ impl<T> Sender<T> {
|
806 | 838 |
|
807 | 839 | #[stable(feature = "rust1", since = "1.0.0")]
|
808 | 840 | impl<T> Clone for Sender<T> {
|
| 841 | + /// Clone a sender to send to other threads. |
| 842 | + /// |
| 843 | + /// Note, be aware of the lifetime of the sender because all senders |
| 844 | + /// (including the original) need to be dropped in order for |
| 845 | + /// [`Receiver::recv`] to stop blocking. |
809 | 846 | fn clone(&self) -> Sender<T> {
|
810 | 847 | let packet = match *unsafe { self.inner() } {
|
811 | 848 | Flavor::Oneshot(ref p) => {
|
@@ -1064,9 +1101,10 @@ impl<T> Receiver<T> {
|
1064 | 1101 | /// corresponding channel has hung up.
|
1065 | 1102 | ///
|
1066 | 1103 | /// This function will always block the current thread if there is no data
|
1067 |
| - /// available and it's possible for more data to be sent. Once a message is |
1068 |
| - /// sent to the corresponding [`Sender`] (or [`SyncSender`]), then this |
1069 |
| - /// receiver will wake up and return that message. |
| 1104 | + /// available and it's possible for more data to be sent (at least one sender |
| 1105 | + /// still exists). Once a message is sent to the corresponding [`Sender`] |
| 1106 | + /// (or [`SyncSender`]), this receiver will wake up and return that |
| 1107 | + /// message. |
1070 | 1108 | ///
|
1071 | 1109 | /// If the corresponding [`Sender`] has disconnected, or it disconnects while
|
1072 | 1110 | /// this call is blocking, this call will wake up and return [`Err`] to
|
@@ -1146,9 +1184,10 @@ impl<T> Receiver<T> {
|
1146 | 1184 | /// corresponding channel has hung up, or if it waits more than `timeout`.
|
1147 | 1185 | ///
|
1148 | 1186 | /// This function will always block the current thread if there is no data
|
1149 |
| - /// available and it's possible for more data to be sent. Once a message is |
1150 |
| - /// sent to the corresponding [`Sender`] (or [`SyncSender`]), then this |
1151 |
| - /// receiver will wake up and return that message. |
| 1187 | + /// available and it's possible for more data to be sent (at least one sender |
| 1188 | + /// still exists). Once a message is sent to the corresponding [`Sender`] |
| 1189 | + /// (or [`SyncSender`]), this receiver will wake up and return that |
| 1190 | + /// message. |
1152 | 1191 | ///
|
1153 | 1192 | /// If the corresponding [`Sender`] has disconnected, or it disconnects while
|
1154 | 1193 | /// this call is blocking, this call will wake up and return [`Err`] to
|
|
0 commit comments