Skip to content

Commit

Permalink
Use easier readme example using channels
Browse files Browse the repository at this point in the history
  • Loading branch information
Jikstra committed Apr 29, 2021
1 parent e53b163 commit 767d0ed
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ A simple easy to use wrapper around Ctrl-C signal.

## Example usage
```rust
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::sync::mpsc::channel;
use ctrlc;

fn main() {
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
let (tx, rx) = channel();

ctrlc::set_handler(move || {
r.store(false, Ordering::SeqCst);
tx.send(()).expect("Could not send signal on channel.")
}).expect("Error setting Ctrl-C handler");

println!("Waiting for Ctrl-C...");
while running.load(Ordering::SeqCst) {}
println!("Got it! Exiting...");
rx.recv().expect("Could not receive from channel.");
println!("Got it! Exiting...");
}
```

Expand Down
22 changes: 9 additions & 13 deletions examples/readme_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@
// notice may not be copied, modified, or distributed except
// according to those terms.

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time;
use std::sync::mpsc::channel;
use ctrlc;

fn main() {
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
let (tx, rx) = channel();

ctrlc::set_handler(move || {
r.store(false, Ordering::SeqCst);
})
.expect("Error setting Ctrl-C handler");
tx.send(()).expect("Could not send signal on channel.")
}).expect("Error setting Ctrl-C handler");

println!("Waiting for Ctrl-C...");
while running.load(Ordering::SeqCst) {
thread::sleep(time::Duration::from_millis(10));
}
println!("Got it! Exiting...");
rx.recv().expect("Could not receive from channel.");
println!("Got it! Exiting...");
}

0 comments on commit 767d0ed

Please sign in to comment.