项目作者: DenisKolodin

项目描述 :
Rust library to control threads and futures execution
高级语言: Rust
项目地址: git://github.com/DenisKolodin/thread-control.git
创建时间: 2016-09-21T12:12:48Z
项目社区:https://github.com/DenisKolodin/thread-control

开源协议:Other

下载


Thread-control library

Missing Rust execution control tools for threads
and futures.

Threads example

  1. use std::thread;
  2. use thread_control::*;
  3. fn main() {
  4. let (flag, control) = make_pair();
  5. let handle = thread::spawn(move || {
  6. while flag.alive() {
  7. }
  8. });
  9. assert_eq!(control.is_done(), false);
  10. control.stop(); // Also you can `control.interrupt()` it
  11. handle.join();
  12. assert_eq!(control.is_interrupted(), false);
  13. assert_eq!(control.is_done(), true);
  14. }

Futures example

  1. let (flag, control) = thread_control::make_pair();
  2. let duration = Duration::from_secs(5);
  3. let alive_checker = Interval::new(duration, &handle).unwrap()
  4. .and_then(move |value| {
  5. if flag.is_alive() {
  6. Ok(value)
  7. } else {
  8. Err(other("stream was interrupted by thread control!"))
  9. }
  10. });
  11. thread::spawn(move || {
  12. // Any routine with `control.is_done()` checking
  13. });
  14. let managed_routine = alive_checker.select(mystream).for_each(|_| Ok(()));
  15. core.run(managed_routine).unwrap();

License

Licensed under either of

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.