What is the correct way in the post C++11 world for setting the priority of an instance of std::thread
Is there a portable way of doing this that works at least in Windows and POSIX (Linux) environments?
Or is it a matter of getting a handle and using whatever native calls are available for the particular OS?
In Windows processes are organized in class and level priority. Read this: Scheduling Priorities, it gives a good overall knowledge about thread and process priority. You can use the following functions to control the priorities even dynamically: GetPriorityClass(), SetPriorityClass(), SetThreadPriority(), GetThreadPriority().
Apperantly you can also use
std::thread
'snative_handle()
withpthread_getschedparam()
orpthread_setschedparam()
on a windows system. Check this example, std::thread: Native Handle and pay attention to the headers added!My quick implementation...
and this is how I use it...
There's no way to set thread priorities via the C++11 library. I don't think this is going to change in C++14, and my crystal ball is too hazy to comment on versions after that.
In POSIX,
pthread_setschedparam(thread.native_handle(), policy, {priority});
I don't know the equivalent Windows function, but I'm sure there must be one.
The standard C++ library doesn't define any access to thread priorities. To set thread attributes you'd use the
std::thread
'snative_handle()
and use it, e.g., on a POSIX system withpthread_getschedparam()
orpthread_setschedparam()
. I don't know if there are any proposals to add scheduling attributes to the thread interface.