1 个不稳定版本
0.1.0 | 2023 年 4 月 7 日 |
---|
在 并发 中排名 #1130
18KB
151 行
HybridFutex
HybridFutex
是一个 Rust 库,它提供了一个同步原语,允许线程等待来自另一个线程的通知。它设计为低开销且可扩展,并支持同步和异步等待及通知。
特性
- 支持在任何目标上执行 Notify many 操作。
- 支持同步和异步等待。
- 内置高竞争场景的公平性和可扩展性。
- 使用 park API 的高效内核辅助阻塞。
- 在 Windows 和 Unix 平台上都具有低延迟的等待和通知。
- 无外部依赖的跨平台兼容性。
- 简单易用的 API。
用法
要使用 HybridFutex
,只需将其添加到您的 Cargo.toml
文件中的依赖项即可
[dependencies]
hybrid-futex = "0.1"
然后,您可以在 Rust 代码中使用 HybridFutex
如下
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use hybridfutex::HybridFutex;
let wait_queue = Arc::new(HybridFutex::new());
let wait_queue_clone = wait_queue.clone();
// Spawn a thread that waits for a notification from another thread
let handle = thread::spawn(move || {
println!("Thread 1 is waiting");
wait_queue_clone.wait_sync();
println!("Thread 1 is notified");
});
// Wait for a short time before notifying the other thread
thread::sleep(Duration::from_millis(100));
// Notify the other thread
wait_queue.notify_one();
// Wait for the other thread to finish
handle.join().unwrap();
许可证
本项目的许可证为 MIT 许可证 - 有关详细信息,请参阅 LICENSE 文件。
致谢
- 此库受 Linux 中的 futex 系统调用的启发。
依赖关系
~155KB