-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds simple single producer, single consumer crossbeam channel exampl…
…e. (#551)
- Loading branch information
Showing
4 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters