Skip to content

Commit

Permalink
Adds simple single producer, single consumer crossbeam channel exampl…
Browse files Browse the repository at this point in the history
…e. (#551)
  • Loading branch information
j-haj authored and AndyGauge committed Dec 17, 2019
1 parent d7c2911 commit 084f51c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/concurrency.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] |
Expand All @@ -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
Expand Down
41 changes: 41 additions & 0 deletions src/concurrency/thread/crossbeam-spsc.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions src/concurrency/threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down

0 comments on commit 084f51c

Please sign in to comment.