在Qt中设置GUI线程的优先级 [英] Set priority to GUI thread in Qt
问题描述
是否可以为主GUI线程设置优先级,使其比其他线程(QThread)具有更高的优先级?
Is it possible to set priority to the main GUI thread so it has higher priority comparing to the other threads (QThread)?
我的目标是在其他线程正在进行大量计算时可能不会冻结GUI,这可能会占用CPU到100%的负载.如果有人可以共享一种方法来确保GUI在此期间不会冻结,而其他计算线程仍可以尝试最大化CPU使用率,那就太好了.
My aim is to not to freeze up the GUI while the other threads are doing some intensive computation which may occupy CPU to 100% load. It would be great if someone can share a way to make sure GUI will not freeze during this period while the other computation threads can still try to maximize the CPU usage.
我考虑过要管理其他线程,所以我不会同时启动太多计算线程.
I thought about managing other threads, so I don't start too many computation threads at the same time.
推荐答案
当当前线程是gui线程时,更改当前线程的优先级:
Change the priority of the current thread when the current thread is the gui thread:
int main(int argc, char ** argv) {
QApplication app(argc, argv);
QThread::currentThread()->setPriority(QThread::HighPriority);
...
}
您也可以从任何其他线程提交更改-只要主(GUI)线程具有正在运行的事件循环:
You can submit the change from any other thread, too – as long as the main (GUI) thread has a running event loop:
QMetaObject::invokeMethod(qApp, []{
QThread::currentThread()->setPriority(QThread::HighPriority);
});
这篇关于在Qt中设置GUI线程的优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!