开发者

Boost: how to set program priority?

How to set program/thread priority (when talking about threads I mean ones that are created using Boost library) using Boost C++开发者_JAVA技巧 library? I mean crossplatform way...


There's no generalised cross-platform priority support in boost itself.

#ifdef is your friend...

See Tom's answer for the win32 solution.

On Linux you'd use the nice call (or possibly setpriority). Don't be put off by those document's statements that they adjust process priority; on Linux a thread is just a process which shares a memory space with some other process/processes. If you get yourself a better version of "top" which lists the individual threads, you can see the nice levels of each.

Note that a process/thread with normal user privileges can only reduce its priority (higher "nice" value) on Linux. The little bit of work with priorities I've done on Windows, it seemed anything goes (but that was on XP; situation may be different post-Vista, UAC etc).


Change boost thread priority in Windows


The previous answer's comment about Windows allowing users to change priorities to anything they want is partly correct, with a couple exceptions -- One, the Real Time priority class is normally not allowed (even with UAC elevation). It requires a special security token. That token is normally available to administrators though, but the process must request it.

Also, in Vista+ at least, raising the priority class of an external process is a right reserved only for Administrators in the default security policy. However, this only applies to EXTERNAL processes - so wouldn't affect your ability to set your own priority class.

Please keep in mind in Windows when you change the process priority class, you are changing the 'base' priority of all the threads in that process. You can leave the priority class at normal and adjust the priorities of specific threads as necessary (SetThreadPriority API vs SetPriorityClass API). This may be preferred for your own application.

I don't think this is the case here, but a warning -- if acting on an external process (not your own), never change the individual thread priorities of another process. Instead, only change the priority class so that all thread priorities retain the same relative difference to one another in scheduling importance.

There is no cross-platform way to do it because the behavior is so different. Conditional compile directives are, as the previous poster suggested, to be used in this situation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜