| 1 |
//! Using `select!` to send and receive on the same channel at the same time. |
| 2 |
//! |
| 3 |
//! This example is based on the following program in Go. |
| 4 |
//! |
| 5 |
//! Source: |
| 6 |
//! - https://web.archive.org/web/20171209034309/https://www.nada.kth.se/~snilsson/concurrency |
| 7 |
//! - http://www.nada.kth.se/~snilsson/concurrency/src/matching.go |
| 8 |
//! |
| 9 |
//! Copyright & License: |
| 10 |
//! - Stefan Nilsson |
| 11 |
//! - Creative Commons Attribution 3.0 Unported License |
| 12 |
//! - https://creativecommons.org/licenses/by/3.0/ |
| 13 |
//! |
| 14 |
//! ```go |
| 15 |
//! func main() { |
| 16 |
//! people := []string{"Anna", "Bob", "Cody", "Dave", "Eva"} |
| 17 |
//! match := make(chan string, 1) // Make room for one unmatched send. |
| 18 |
//! wg := new(sync.WaitGroup) |
| 19 |
//! for _, name := range people { |
| 20 |
//! wg.Add(1) |
| 21 |
//! go Seek(name, match, wg) |
| 22 |
//! } |
| 23 |
//! wg.Wait() |
| 24 |
//! select { |
| 25 |
//! case name := <-match: |
| 26 |
//! fmt.Printf("No one received %s’s message.\n", name) |
| 27 |
//! default: |
| 28 |
//! // There was no pending send operation. |
| 29 |
//! } |
| 30 |
//! } |
| 31 |
//! |
| 32 |
//! // Seek either sends or receives, whichever possible, a name on the match |
| 33 |
//! // channel and notifies the wait group when done. |
| 34 |
//! func Seek(name string, match chan string, wg *sync.WaitGroup) { |
| 35 |
//! select { |
| 36 |
//! case peer := <-match: |
| 37 |
//! fmt.Printf("%s received a message from %s.\n", name, peer) |
| 38 |
//! case match <- name: |
| 39 |
//! // Wait for someone to receive my message. |
| 40 |
//! } |
| 41 |
//! wg.Done() |
| 42 |
//! } |
| 43 |
//! ``` |
| 44 |
|
| 45 |
use crossbeam_channel::{bounded, select}; |
| 46 |
use crossbeam_utils::thread; |
| 47 |
|
| 48 |
fn main() { |
| 49 |
let people = vec!["Anna", "Bob", "Cody", "Dave", "Eva"]; |
| 50 |
let (s, r) = bounded(1); // Make room for one unmatched send. |
| 51 |
|
| 52 |
// Either send my name into the channel or receive someone else's, whatever happens first. |
| 53 |
let seek = |name, s, r| { |
| 54 |
select! { |
| 55 |
recv(r) -> peer => println!("{} received a message from {}.", name, peer.unwrap()), |
| 56 |
send(s, name) -> _ => {}, // Wait for someone to receive my message. |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
thread::scope(|scope| { |
| 61 |
for name in people { |
| 62 |
let (s, r) = (s.clone(), r.clone()); |
| 63 |
scope.spawn(move |_| seek(name, s, r)); |
| 64 |
} |
| 65 |
}) |
| 66 |
.unwrap(); |
| 67 |
|
| 68 |
// Check if there is a pending send operation. |
| 69 |
if let Ok(name) = r.try_recv() { |
| 70 |
println!("No one received {}’s message.", name); |
| 71 |
} |
| 72 |
} |