https://github.com/rustonaut/fuseflag
A (not resettable) fuse/flag which can be shared between different threads
https://github.com/rustonaut/fuseflag
Last synced: about 1 year ago
JSON representation
A (not resettable) fuse/flag which can be shared between different threads
- Host: GitHub
- URL: https://github.com/rustonaut/fuseflag
- Owner: rustonaut
- License: apache-2.0
- Created: 2016-09-08T20:32:46.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-09-09T18:16:22.000Z (almost 10 years ago)
- Last Synced: 2025-01-30T20:14:51.820Z (over 1 year ago)
- Language: Rust
- Size: 16.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# fuseflag [](https://travis-ci.org/dathinab/fuseflag)
**A (not resettable) fuse/flag which can be shared between different threads**
---
Fuseflag provides a Fuse/Flag which can be shared (by cloning) between multiple threads.
The Fuse/Flag can be checked and burned, once burned it cannot be restored.
A additional functionality is a wrapper around `thread::spawn` which takes a closure
expecting a `FuseFlag` as single parameter and returns a Wrapper around `JoinHandle`
which burns the fuse before calling join.
## Example
```rust
extern crate fuseflag;
use std::time::Duration;
use std::thread::{sleep, spawn, JoinHandle};
use fuseflag::FuseFlag;
fn background_job(end_flag: FuseFlag, id: u64) -> JoinHandle<()> {
spawn(move || {
while end_flag.check() {
println!("\tbg-job({}): continues workin", id);
//Background work Work
sleep(Duration::from_millis(430*id));
}
println!("\tbg-job({}) stops running", id);
})
}
fn main() {
let end_fuse = FuseFlag::new();
let bg_guard = vec![
background_job(end_fuse.clone(), 1),
background_job(end_fuse.clone(), 2)
];
println!("running fake ui loop...");
//naturally a very nice ui called sleep ;)
sleep(Duration::from_secs(3));
println!("main loop ended: waiting for background jobs to stop");
//this will trigger .check() to return false (for ever from now on)
end_fuse.burn();
//join the threads
for guard in bg_guard {
guard.join().unwrap();
}
}
```
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any
additional terms or conditions.