5

我正在尝试在 rust 中设置一个简单的计时器,它以一定的频率返回 true。

#[derive(Clone, Debug)]
pub struct IntervalTimer {
    pub period: Duration,
    pub delta: Instant,
}

impl IntervalTimer {
    pub fn new(period: Duration) -> Self {
        let delta = Instant::now();
        Self { period, delta }
    }

    /// Returns true if the interval between calls has exceeded the period
    pub fn ready(&mut self) -> bool {
        if self.delta.elapsed() < self.period {
            false
        } else {
            self.delta = self.delta + self.period;
            true
        }
    }
}

std::time::Instant出于某种原因,在任务内使用时,这似乎总是会出现恐慌rayon(如果重要的话,在 Legion ECS 中运行)。

这是唯一一篇包含相同信息的帖子,但归结为使用了不寻常的内核。我只是使用 Ubuntu 18.04,没有任何异常设置。

此外,我尝试了多台机器,但代码出现了同样的错误。

这与使用std::time::Instant和有关rayon吗?

thread 'thread '<unnamed><unnamed>' panicked at '' panicked at 'supplied instant is later than self', src/libstd/time.rs:263:9
stack backtrace:
supplied instant is later than self', src/libstd/time.rs:263:9
   0:     0x5574c8e3b9e4 - backtrace::backtrace::libunwind::trace::hc1c4a1d8ad423b97
                               at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/libunwind.rs:86
   1:     0x5574c8e3b9e4 - backtrace::backtrace::trace_unsynchronized::h82274781060cb056
                               at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/mod.rs:66
   2:     0x5574c8e3b9e4 - std::sys_common::backtrace::_print_fmt::h2a45d89b653a4da8
                               at src/libstd/sys_common/backtrace.rs:78
   3:     0x5574c8e3b9e4 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h41a0a93ab85e6aa1
                               at src/libstd/sys_common/backtrace.rs:59
   4:     0x5574c8e64b2c - core::fmt::write::hdaea18585065a96d
                               at src/libcore/fmt/mod.rs:1069
   5:     0x5574c8e34743 - std::io::Write::write_fmt::h0cea70c809005252
                               at src/libstd/io/mod.rs:1504
   6:     0x5574c8e3e7a5 - std::sys_common::backtrace::_print::hd95f9978cc145ca4
                               at src/libstd/sys_common/backtrace.rs:62
   7:     0x5574c8e3e7a5 - std::sys_common::backtrace::print::hfb25ca2291be47d0
                               at src/libstd/sys_common/backtrace.rs:49
   8:     0x5574c8e3e7a5 - std::panicking::default_hook::{{closure}}::h44f76cee5dc8591c
                               at src/libstd/panicking.rs:198
   9:     0x5574c8e3e4e2 - std::panicking::default_hook::h198e1a712910f1e6
                               at src/libstd/panicking.rs:218
  10:     0x5574c8e3ee02 - std::panicking::rust_panic_with_hook::hc0b4730bb8013f9d
                               at src/libstd/panicking.rs:511
  11:     0x5574c8e3e9eb - rust_begin_unwind
                               at src/libstd/panicking.rs:419
  12:     0x5574c8e62921 - core::panicking::panic_fmt::h1ac71ad045d55416
                               at src/libcore/panicking.rs:111
  13:     0x5574c8e626b3 - core::option::expect_failed::h7baa1c60813ff0e3
                               at src/libcore/option.rs:1260
  14:     0x5574c8e3b3e5 - core::option::Option<T>::expect::h306e330bd810d3ba
                               at /rustc/49cae55760da0a43428eba73abcb659bb70cf2e4/src/libcore/option.rs:347
  15:     0x5574c8e3b3e5 - std::time::Instant::duration_since::hba1f841dfadd1cd2
                               at src/libstd/time.rs:263
  16:     0x5574c8e3b3e5 - <std::time::Instant as core::ops::arith::Sub>::sub::h73cd3ea7a55658e1
                               at src/libstd/time.rs:390
  17:     0x5574c8e3b3e5 - std::time::Instant::elapsed::h67e85e1d84cb14d0
                               at src/libstd/time.rs:327
  18:     0x5574c8bfe09f - locus_sim_engine::utils::interval_timer::IntervalTimer::ready::h7569661c5fd39089
                               at src/utils/interval_timer.rs:17
  19:     0x5574c8bfe09f - locus_sim_engine::system::laser_scan::single_laser_scan::h671b01a54874470e
                               at src/system/laser_scan.rs:63
  20:     0x5574c8bf074d - locus_sim_engine::system::laser_scan::laser_scan::{{closure}}::h1deba93ec7418c4d
                               at src/system/laser_scan.rs:50
  21:     0x5574c8bf074d - core::ops::function::impls::<impl core::ops::function::Fn<A> for &F>::call::h96b179e6c0dd1ac0
                               at /home/michael/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/ops/function.rs:242
  22:     0x5574c8bf074d - core::ops::function::impls::<impl core::ops::function::FnMut<A> for &F>::call_mut::h4f8d7ff151c5feb5
                               at /home/michael/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/ops/function.rs:252
  23:     0x5574c8a918f8 - <locus_sim_api::motor_server::MotorServer<T> as tower_service::Service<http::request::Request<B>>>::call::{{closure}}::hf5b46adb51805ebe                                         
                               at /home/michael/locus_dev/src/locus_sim_engine/target/release/build/locus-sim-api-4d28cb656733f563/out/locus_sim_api.rs:531                                                 
  24:     0x5574c8a918f8 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h9798dbde38a1f607                                                                            
                               at /home/michael/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/future/mod.rs:66                                                        
  25:     0x5574c8a104ad - <core::pin::Pin<P> as core::future::future::Future>::poll::h6aa9c7207adebc07                                                                                                     
                               at /home/michael/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/future/future.rs:118                                                    
  26:     0x5574c8a104ad - <F as futures_core::future::TryFuture>::try_poll::h5633e6d6a71350d5                                                                                                              
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-core-0.3.5/src/future.rs:83                                                                         
  27:     0x5574c8a104ad - <futures_util::future::try_future::into_future::IntoFuture<Fut> as core::future::future::Future>::poll::h20812bb48a44deb6                                                        
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/future/try_future/into_future.rs:31                                                  
  28:     0x5574c8a104ad - <futures_util::future::future::map::Map<Fut,F> as core::future::future::Future>::poll::h7de76b6b06dabc84                                                                         
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/future/future/map.rs:67                                                              
  29:     0x5574c8a10298 - <futures_util::future::future::Map<Fut,F> as core::future::future::Future>::poll::h87636451ef52bf16                                                                              
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/lib.rs:107                                                                           
  30:     0x5574c8a10298 - <futures_util::future::try_future::MapErr<Fut,F> as core::future::future::Future>::poll::hf361a24bd15b64de                                                                       
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/lib.rs:107                                                                           
  31:     0x5574c8a10298 - <futures_util::future::either::Either<A,B> as core::future::future::Future>::poll::h3ba6a79df0fece24                                                                             
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/future/either.rs:65                                                                  
  32:     0x5574c8a10298 - <F as futures_core::future::TryFuture>::try_poll::h5d9826bce7bec57c                                                                                                              
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-core-0.3.5/src/future.rs:83                                                                         
  33:     0x5574c8a10298 - <futures_util::future::try_future::into_future::IntoFuture<Fut> as core::future::future::Future>::poll::h8cc4e2435674ca81                                                        
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/future/try_future/into_future.rs:31                                                  
  34:     0x5574c8a10298 - <futures_util::future::future::map::Map<Fut,F> as core::future::future::Future>::poll::h3a97595fa35bf6b4                                                                         
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/future/future/map.rs:67                                                              
  35:     0x5574c8a10645 - <futures_util::future::future::Map<Fut,F> as core::future::future::Future>::poll::h1f03e91c826fb89f                                                                              
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/lib.rs:107                                                                           
  36:     0x5574c8a10645 - <futures_util::future::try_future::MapErr<Fut,F> as core::future::future::Future>::poll::hbb30c6370d1ad4dd                                                                       
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/lib.rs:107                                                                           
  37:     0x5574c8a10645 - <futures_util::future::either::Either<A,B> as core::future::future::Future>::poll::h45a493d70ab6cb16                                                                             
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/future/either.rs:66                                                                  
  38:     0x5574c8a10645 - <F as futures_core::future::TryFuture>::try_poll::h3218040a42683b8c                                                                                                              
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-core-0.3.5/src/future.rs:83                                                                         
  39:     0x5574c8a10645 - <futures_util::future::try_future::into_future::IntoFuture<Fut> as core::future::future::Future>::poll::hffacf042cca96add                                                        
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/future/try_future/into_future.rs:31                                                  
  40:     0x5574c8a10645 - <futures_util::future::future::map::Map<Fut,F> as core::future::future::Future>::poll::hd3999158d5641280                                                                         
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/future/future/map.rs:67                                                              
  41:     0x5574c8a0fca0 - <futures_util::future::future::Map<Fut,F> as core::future::future::Future>::poll::he914f5b3eab7d7ff                                                                              
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/lib.rs:107                                                                           
  42:     0x5574c8a0fca0 - <futures_util::future::try_future::MapErr<Fut,F> as core::future::future::Future>::poll::haa236b3c8a953e0b                                                                       
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/lib.rs:107                                                                           
  43:     0x5574c8a0fca0 - <futures_util::future::either::Either<A,B> as core::future::future::Future>::poll::h79e46a6c42aa3fc0                                                                             
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/future/either.rs:66                                                                  
  44:     0x5574c8a0fca0 - <F as futures_core::future::TryFuture>::try_poll::h11442ab0450f9aba                                                                                                              
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-core-0.3.5/src/future.rs:83                                                                         
  45:     0x5574c8a0fca0 - <futures_util::future::try_future::into_future::IntoFuture<Fut> as core::future::future::Future>::poll::h4514bbaa544f1a54                                                        
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/future/try_future/into_future.rs:31                                                  
  46:     0x5574c8a0fca0 - <futures_util::future::future::map::Map<Fut,F> as core::future::future::Future>::poll::h3467a48e5d1b449e                                                                         
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/future/future/map.rs:67                                                              
  47:     0x5574c8a0f5e5 - <futures_util::future::future::Map<Fut,F> as core::future::future::Future>::poll::hc132b12a66e846ba                                                                              
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/lib.rs:107                                                                           
  48:     0x5574c8a0f5e5 - <futures_util::future::try_future::MapErr<Fut,F> as core::future::future::Future>::poll::hbbbc3932986b69ae                                                                       
                               at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/lib.rs:107
4

2 回答 2

10

你的代码是完全正确的。您应该向https://github.com/rust-lang/rust提交错误。Windows 和 ARM Linux 已经有类似问题的解决方法。

于 2021-02-26T15:30:27.913 回答
2

我的猜测是,任务正在跳转到不同的内核,并且它们的时钟不同步,从而导致非单调性。

elapsed()只是计算now() - *self 所以而不是检查elapsed() > period检查是否now() > delta + period

于 2020-10-01T20:37:40.853 回答