Adds simple single producer, single consumer crossbeam channel example. (#551)

This commit is contained in:
Jeff Hajewski 2019-12-16 20:22:36 -06:00 committed by Andrew Gauger
parent d7c291151f
commit 084f51cff9
4 changed files with 46 additions and 0 deletions

View file

@ -16,6 +16,7 @@ cc = "1.0"
chrono = "0.4"
clap = "2.29"
crossbeam = "0.5"
crossbeam-channel = "0.3.9"
csv = "1.0"
data-encoding = "2.1.0"
env_logger = "0.5"

View file

@ -3,6 +3,7 @@
| Recipe | Crates | Categories |
|--------|--------|------------|
| [Spawn a short-lived thread][ex-crossbeam-spawn] | [![crossbeam-badge]][crossbeam] | [![cat-concurrency-badge]][cat-concurrency] |
| [Pass data between two threads][ex-crossbeam-spsc] | [![crossbeam-badge]][crossbeam] | [![cat-concurrency-badge]][cat-concurrency] |
| [Maintain global mutable state][ex-global-mut-state] | [![lazy_static-badge]][lazy_static] | [![cat-rust-patterns-badge]][cat-rust-patterns] |
| [Calculate SHA1 sum of *.iso files concurrently][ex-threadpool-walk] | [![threadpool-badge]][threadpool] [![walkdir-badge]][walkdir] [![num_cpus-badge]][num_cpus] [![ring-badge]][ring] | [![cat-concurrency-badge]][cat-concurrency][![cat-filesystem-badge]][cat-filesystem] |
| [Draw fractal dispatching work to a thread pool][ex-threadpool-fractal] | [![threadpool-badge]][threadpool] [![num-badge]][num] [![num_cpus-badge]][num_cpus] [![image-badge]][image] | [![cat-concurrency-badge]][cat-concurrency][![cat-science-badge]][cat-science][![cat-rendering-badge]][cat-rendering] |
@ -15,6 +16,7 @@
[ex-crossbeam-spawn]: concurrency/threads.html#spawn-a-short-lived-thread
[ex-crossbeam-spsc]: concurrency/threads.html#pass-data-between-two-threads
[ex-global-mut-state]: concurrency/threads.html#maintain-global-mutable-state
[ex-threadpool-walk]: concurrency/threads.html#calculate-sha1-sum-of-iso-files-concurrently
[ex-threadpool-fractal]: concurrency/threads.html#draw-fractal-dispatching-work-to-a-thread-pool

View file

@ -0,0 +1,41 @@
# Pass data between two threads
[![crossbeam-badge]][crossbeam] [![cat-concurrency-badge]][cat-concurrency]
This example demonstrates the use of [crossbeam-channel] in a single producer, single
consumer (SPSC) setting. We build off the [ex-crossbeam-spawn] example by using
[`crossbeam::scope`] and [`Scope::spawn`] to manage the producer thread. Data is
exchanged between the two threads using a [`crossbeam_channel::unbounded`]
channel, meaning there is no limit to the number of storeable messages. The
producer thread sleeps for half a second in between messages.
```rust
extern crate crossbeam;
extern crate crossbeam_channel;
use std::{thread, time};
use crossbeam_channel::unbounded;
fn main() {
let (snd, rcv) = unbounded();
let n_msgs = 5;
crossbeam::scope(|s| {
s.spawn(|_| {
for i in 0..n_msgs {
snd.send(i).unwrap();
thread::sleep(time::Duration::from_millis(100));
}
});
}).unwrap();
for _ in 0..n_msgs {
let msg = rcv.recv().unwrap();
println!("Received {}", msg);
}
}
```
[crossbeam-channel]: https://docs.rs/crate/crossbeam-channel/
[ex-crossbeam-spawn]: concurrency/threads.html#spawn-a-short-lived-thread
[`crossbeam::scope`]: https://docs.rs/crossbeam/*/crossbeam/fn.scope.html
[`Scope::spawn`]: https://docs.rs/crossbeam/*/crossbeam/thread/struct.Scope.html#method.spawn
[`crossbeam_channel::unbounded`]: https://docs.rs/crossbeam-channel/*/crossbeam_channel/fn.unbounded.html

View file

@ -2,6 +2,8 @@
{{#include thread/crossbeam-spawn.md}}
{{#include thread/crossbeam-spsc.md}}
{{#include thread/global-mut-state.md}}
{{#include thread/threadpool-walk.md}}