Crates.io | hipthread |
lib.rs | hipthread |
version | 0.1.2 |
source | src |
created_at | 2024-12-20 02:20:01.944795 |
updated_at | 2025-01-23 12:47:04.854428 |
description | no-std thread library based on pthread |
homepage | |
repository | https://gitcode.com/xuanwu/hipthread |
max_upload_size | |
id | 1489751 |
size | 55,507 |
在no-std环境下,封装unix的pthread和mingw的winpthread, 可支持unix和windows.
LocalKey<T>
: 封装ThrdLocal
,更方便使用static KEY: LocalKey<i32> = LocalKey::new();
fn main() {
assert!(KEY.get().is_null());
KEY.set(&101);
spawn(|| {
assert!(KEY.get().is_null());
KEY.set(&102);
}).unwrap().join();
assert_eq!(KEY.replace(core::ptr::null()), &101);
}
LocalRefCell<T>
: 提供RefCell<T>
类型的TLS变量操作接口LocalCell<T>
: 提供Cell<T>
类型的TLS变量操作接口static LOCAL: LocalCell<i32> = LocalCell::new(|| 100);
fn main() {
assert_eq!(LOCAL.get(), 100);
LOCAL.replace(200);
let handle = spawn(|| {
LOCAL.replace(300);
LOCAL.get()
}).unwrap();
assert_eq!(handle.join().unwrap(), 300);
assert_eq!(LOCAL.get(), 200);
}
spawn/spawn_with
: 创建线程ThrdLocal
: TLS变量的存取.Mutex
: 互斥锁Once/OnceLock/LazyLock
: 类似std库中的同名类功能.sched_cpu_count
: 获取当前进程可用的核数量.sched_getaffinity/sched_setaffinity
: 设置当前进程同cpu核的亲和性thrd_setaffinity
: 设置当前线程同cpu核的亲和性.thrd_setname/thrd_getname
: 设置获取当前线程的名字,方便调测.