- Notifications
You must be signed in to change notification settings - Fork22
A simple Cross-platform thread schedule and priority library for rust.
License
NotificationsYou must be signed in to change notification settings
iddm/thread-priority
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A simple library to control thread schedule policies and thread priority.
If your operating system isn't yet supported, please, create an issue.
Is1.67
. If you need any help making it possible to compile with1.56
please reach out, itshould be possible by just downgrading therstest
version to0.17
or lower (the code iscompatible).
- Linux
- Android
- DragonFly
- FreeBSD
- OpenBSD
- NetBSD
- macOS
- iOS
- Windows
Setting current thread's priority to minimum:
use thread_priority::*;fnmain(){assert!(set_current_thread_priority(ThreadPriority::Min).is_ok());}
The same as above but using a specific value:
use thread_priority::*;use std::convert::TryInto;fnmain(){// The lower the number the lower the priority.assert!(set_current_thread_priority(ThreadPriority::Crossplatform(0.try_into().unwrap())).is_ok());}
Set the thread priority to the lowest possible value:
use thread_priority::*;fnmain(){// The lower the number the lower the priority.assert!(set_current_thread_priority(ThreadPriority::Os(WinAPIThreadPriority::Lowest.into())).is_ok());}
Set the ideal processor for the new thread:
use thread_priority::*;fnmain(){ std::thread::spawn(||{set_thread_ideal_processor(thread_native_id(),0);println!("Hello world!");});}
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();
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();
use thread_priority::*;assert!(std::thread::current().get_priority().is_ok());println!("This thread's native id is: {:?}", std::thread::current().get_native_id());
This project islicensed under the MIT license.