Change boost thread priority in Windows Change boost thread priority in Windows multithreading multithreading

Change boost thread priority in Windows


Use SetThreadPriority function to set the thread priority. SetPriorityClass is used to set the priority of the process. You also have to change the priority values, see documentation for SetThreadPriority for details.


The SetPriorityClass function takes as it's first parameter a HANDLE, you are passing in a pointer to a HANDLE. Change it to:

res = SetPriorityClass(*th, REALTIME_PRIORITY_CLASS);

or something equivalent. The kernel can tell that the pointer value you passed in isn't really a valid thread handle because I guess it maintains an internal list of currently allocated thread handles. The pointer obviously isn't in that list. The compiler can't really enforce better type safety, since a HANDLE is kind of an opaque type - you just have to be really careful what you pass in.

Oh by the way, the other commenter Dani is correct, SetPriorityClass is not used for setting the priority of a thread, you want to use SetThreadPriority anyway. But then my advice would still stand, you need to pass in a HANDLE, not a pointer to such.