30 个版本 (2 个稳定版)

1.1.0 2024年4月29日
0.16.0 2024年2月28日
0.15.1 2023年12月5日
0.15.0 2023年11月13日
0.1.0 2017年6月22日

并发 中排名第 21

Download history · Rust 包仓库 18243/week @ 2024-05-02 · Rust 包仓库 20300/week @ 2024-05-09 · Rust 包仓库 22269/week @ 2024-05-16 · Rust 包仓库 23439/week @ 2024-05-23 · Rust 包仓库 25557/week @ 2024-05-30 · Rust 包仓库 22790/week @ 2024-06-06 · Rust 包仓库 21179/week @ 2024-06-13 · Rust 包仓库 22642/week @ 2024-06-20 · Rust 包仓库 24156/week @ 2024-06-27 · Rust 包仓库 15271/week @ 2024-07-04 · Rust 包仓库 14854/week @ 2024-07-11 · Rust 包仓库 15954/week @ 2024-07-18 · Rust 包仓库 16107/week @ 2024-07-25 · Rust 包仓库 16119/week @ 2024-08-01 · Rust 包仓库 13760/week @ 2024-08-08 · Rust 包仓库 12853/week @ 2024-08-15 · Rust 包仓库

每月下载量 61,613
32 Crates 中使用(直接使用 20 个)

MIT 许可证

89KB
1K SLoC

thread-priority

CI Crates Docs MIT licensed

一个简单的库,用于控制线程调度策略和线程优先级。

如果你的操作系统尚未支持,请创建一个问题。

最小 Rust 编译器版本

1.67。如果您需要任何帮助使其能够使用 1.56 编译,请联系我,只需将 rstest 版本降级到 0.17 或更低(代码是兼容的)即可。

支持的平台

  • Linux
  • Android
  • DragonFly
  • FreeBSD
  • OpenBSD
  • NetBSD
  • macOS
  • iOS
  • Windows

示例

最小跨平台示例

将当前线程的优先级设置为最低

use thread_priority::*;

fn main() {
    assert!(set_current_thread_priority(ThreadPriority::Min).is_ok());
}

与上面相同,但使用特定的值

use thread_priority::*;
use std::convert::TryInto;

fn main() {
    // The lower the number the lower the priority.
    assert!(set_current_thread_priority(ThreadPriority::Crossplatform(0.try_into().unwrap())).is_ok());
}

Windows 特定示例

将线程优先级设置为可能的最小值

use thread_priority::*;

fn main() {
    // The lower the number the lower the priority.
    assert!(set_current_thread_priority(ThreadPriority::Os(WinAPIThreadPriority::Lowest.into())).is_ok());
}

设置新线程的理想处理器

use thread_priority::*;

fn main() {
    std::thread::spawn(|| {
        set_thread_ideal_processor(thread_native_id(), 0);
        println!("Hello world!");
    });
}

使用 ThreadBuilderExt 特性构建线程

use thread_priority::*;
use thread_priority::ThreadBuilderExt;

let thread = std::thread::Builder::new()
    .name("MyNewThread".to_owned())
    .spawn_with_priority(ThreadPriority::Max, |result| {
        // This is printed out from within the spawned thread.
        println!("Set priority result: {:?}", result);
        assert!(result.is_ok());
}).unwrap();
thread.join();

使用 ThreadBuilder 构建线程

use thread_priority::*;

let thread = ThreadBuilder::default()
    .name("MyThread")
    .priority(ThreadPriority::Max)
    .spawn(|result| {
        // This is printed out from within the spawned thread.
        println!("Set priority result: {:?}", result);
        assert!(result.is_ok());
}).unwrap();
thread.join();

// Another example where we don't care about the priority having been set.
let thread = ThreadBuilder::default()
    .name("MyThread")
    .priority(ThreadPriority::Max)
    .spawn_careless(|| {
        // This is printed out from within the spawned thread.
        println!("We don't care about the priority result.");
}).unwrap();
thread.join();

在当前线程上使用 ThreadExt 特性

use thread_priority::*;

assert!(std::thread::current().get_priority().is_ok());
println!("This thread's native id is: {:?}", std::thread::current().get_native_id());

许可证

此项目遵循 MIT 许可证

依赖关系

~130–385KB