如何在C++中设置进程优先级 [英] How to set the process priority in C++

查看:141
本文介绍了如何在C++中设置进程优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个对数据进行排序的程序,我需要将该进程设置为优先级 31,我认为这是 Windows 中最高的进程优先级.我做了一些研究,但不知道如何在 C++ 中做到这一点.

I am working on a program to sort data, and I need to to set the process to priority 31, which I believe is the highest process priority in Windows. I have done some research, but can't figure out how to do it in C++.

推荐答案

Windows API 调用 SetPriorityClass 允许您更改进程优先级,请参阅 MSDN 文档中的示例,并使用 REALTIME_PRIORITY_CLASS 设置最高优先级:

The Windows API call SetPriorityClass allows you to change your process priority, see the example in the MSDN documentation, and use REALTIME_PRIORITY_CLASS to set the highest priority:

SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)

注意:如果您要求真正的实时优先级,您会得到它.这是核弹.操作系统会毫不留情地对实时优先级线程进行优先级排序,甚至远高于操作系统级输入处理、磁盘缓存刷新和其他高优先级时间关键任务.如果您的实时线程耗尽了您的 CPU 容量,您可以轻松锁定整个系统.这样做时要小心,除非绝对必要,否则请考虑使用高优先级.更多信息

Caution: if you are asking for true realtime priority, you are going to get it. This is a nuke. The OS will mercilessly prioritize a realtime priority thread, well above even OS-level input processing, disk-cache flushing, and other high-priority time-critical tasks. You can easily lock up your entire system if your realtime thread(s) drain your CPU capacity. Be cautious when doing this, and unless absolutely necessary, consider using high-priority instead. More information

这篇关于如何在C++中设置进程优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆