by eigenvalue · 3y ago · 0 comments
fn create_low_priority_pool() -> rayon::ThreadPool {
ThreadPoolBuilder::new()
.start_handler(|_| {
#[cfg(target_os = "linux")]
{
use libc::{sched_param, sched_setscheduler, SCHED_IDLE};
unsafe {
let mut param: sched_param = std::mem::zeroed();
param.sched_priority = 0;
sched_setscheduler(0, SCHED_IDLE, ¶m);
}
}
#[cfg(target_os = "windows")]
{
use winapi::um::processthreadsapi::SetThreadPriority;
use winapi::um::winbase::THREAD_PRIORITY_LOWEST;
unsafe {
SetThreadPriority(std::ptr::null_mut(), THREAD_PRIORITY_LOWEST);
}
}
})
.build()
.unwrap()
}
But I don't know how that would work for MacOS. Anyway, it's something to think about.I had a problem recently where a library started a process per CPU thread I suspect the author didn't intend it to start start 256...
Affinity is only easy to get on some platforms but you should use it instead of the system threads if available.
Then in your Python wrapper you can use the wrapper, but your users who care can provide the pool they’ve already created.
Bonus points if it’s an interface or trait that the user can adapt their own scheduler to, if they don’t use rayon. But, I can see how that could be tricky if the design of your library is tightly coupled with the threading solution.
One way to frame it is that thread creation is a significant side effect.