diff --git a/Cargo.toml b/Cargo.toml index 9f33df9..ae7c133 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/concurrency.md b/src/concurrency.md index 44bd809..004805c 100644 --- a/src/concurrency.md +++ b/src/concurrency.md @@ -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 diff --git a/src/concurrency/thread/crossbeam-spsc.md b/src/concurrency/thread/crossbeam-spsc.md new file mode 100644 index 0000000..c46ec0a --- /dev/null +++ b/src/concurrency/thread/crossbeam-spsc.md @@ -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 diff --git a/src/concurrency/threads.md b/src/concurrency/threads.md index 8b7a261..aac649f 100644 --- a/src/concurrency/threads.md +++ b/src/concurrency/threads.md @@ -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}}