When using std::async, what is the best way to set priority? I realize this is platform dependent, but with a posix compliant operating system, would this work?
// Launch task with priority 8
auto future = std::async(std::launch::async, // async policy required
[] ()
{
pthread_setschedprio(pthread_self(), 8);
// do work
});
I've found answers about std::thread, but not for std::async.
Edit. In my case I am on a QNX operating system. So I believe setting the priority like I've done above is valid. However, it does seem like there are valid concerns raised about whether or not the priority will persist after the async task is complete (depending how async is implemented).
Edit2 Potential options seem to be...
I suspect your design is a bad idea.
std::async
is required to behave as-if it was a new std::thread
. But it doesn't have to be on a new pthread. The implementation is free to (and probably should) have a set of underlying threads that it recycles for std::async
-- just clean up thread_local
storage and handle stuff like set_value_at_thread_exit
it would work (under the standard).
Accessing the underlying pthread and messing with its priority, in that case, might impact later async tasks.
I cannot state certainly this would happen, but I found on at least one system that there is an underlying thread pool -- the symptom was that after a certain number of std::async
s new ones waited to be scheduled for old ones to finish. On a low-core system, this caused a deadlock in some insufficiently paranoid code (the dependency chain of waiting for other threads got larger than the thread pool size, so the last task wasn't scheduled). That deadlock shouldn't happen under the standard; but it is evidence that platforms do use thread pools for async.
Consider making your own thread pool on top of std::thread
, and implementing something async
-like on top of it with a priority. If you are keeping std::thread
s around and managing the priority yourself that is less likely to cause problems.
For std::thread
, you can get its native_handle
, which on your system is probably a pthread handle.