百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

spin-sleep

> 编程语言
开源

Rust 精确睡眠。尽可能使用原生睡眠,然后旋转。

190 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Rust 精确睡眠。尽可能使用原生睡眠,然后旋转。

spin_sleep

Accurate sleeping. Only use native sleep as far as it can be trusted, then spin.

The problem with thread::sleep is it isn't always very accurate, and this accuracy varies on platform and state. Spinning is as accurate as we can get, but consumes the CPU rather ungracefully.

This library adds a middle ground, using a configurable native accuracy setting allowing thread::sleep to wait the bulk of a sleep time, and spin the final section to guarantee accuracy.

SpinSleeper

The simplest usage with default native accuracy is a drop in replacement for thread::sleep.

rust
spin_sleep::sleep(Duration::new(1, 12_550_000));

Configure

More advanced usage, including setting a custom native accuracy, can be achieved by constructing a SpinSleeper.

rust
// Create a new sleeper that trusts native thread::sleep with 100μs accuracy
let spin_sleeper = spin_sleep::SpinSleeper::new(100_000)
    .with_spin_strategy(spin_sleep::SpinStrategy::YieldThread);

// Sleep for 1.01255 seconds, this will:
//  - thread:sleep for 1.01245 seconds, i.e., 100μs less than the requested duration
//  - spin until total 1.01255 seconds have elapsed
spin_sleeper.sleep(Duration::new(1, 12_550_000));

Sleep can also be requested in f64 seconds or u64 nanoseconds (useful when used with time crate)

rust
spin_sleeper.sleep_s(1.01255);
spin_sleeper.sleep_ns(1_012_550_000);

OS-specific default settings should be good enough for most cases.

rust
let sleeper = SpinSleeper::default();

Windows Accuracy

Windows (>= Windows 10, version 1803) will use a high resolution waitable timer, similar to sleep in rust std >= 1.75.

Earlier versions of Windows have particularly poor accuracy by default (~15ms), spin_sleep will automatically select the best accuracy on Windows generally achieving ~1-2ms native sleep accuracy.

Minimum supported rust compiler

This crate is maintained with latest stable rust.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rust

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言