multithreading 主线程忙时如何使Qt工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1386043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:04:09  来源:igfitidea点击:

How to make Qt work when main thread is busy?

multithreadingqt

提问by Basilevs

Main (function main is there) thread of my program is reserved for non-GUI tasks. It calls a number of lengthy calculation functions. All implemented GUI's have been doing their work in a separate threads.

我的程序的主(函数 main 在那里)线程是为非 GUI 任务保留的。它调用了许多冗长的计算函数。所有实现的 GUI 都在单独的线程中工作。

I'm now going to implement one more GUI using Qt. Qt documentationsays all GUI related tasks should be done in main thread. In my case, inserting occasional QCoreApplication::processEvents() calls in main thread would be virtually useless due to great delays between them.

我现在要使用 Qt 再实现一个 GUI。Qt 文档说所有与 GUI 相关的任务都应该在主线程中完成。就我而言,在主线程中偶尔插入 QCoreApplication::processEvents() 调用实际上是无用的,因为它们之间的延迟很大。

Is there any way to overcome this constraint of Qt? Is it impossible to do something non-GUI related in main thread of Qt program?

有没有办法克服Qt的这种限制?在 Qt 程序的主线程中做一些非 GUI 相关的事情是不可能的吗?

采纳答案by Thomi

No, you should be doing your calculations in a separate thread. As you already mentioned, there is a work-around available in QCoreApplication::processEvents(), but it sounds like you're unable to make that work for you.

不,您应该在单独的线程中进行计算。正如您已经提到的,在 中有一个解决方法QCoreApplication::processEvents(),但听起来您无法为您工作。

If you don't want to have to do all the work of setting up a QThread and moving all your code, you may find that the QtConcurrent::runfunction is useful - it allows you to run a function asynchronously.

如果您不想完成设置 QThread 和移动所有代码的所有工作,您可能会发现QtConcurrent::run函数很有用 - 它允许您异步运行函数。

A few pointers: You should try and keep your main (GUI) thread as light as possible. Large amounts of IO or calculations should either be done asynchronously using QtConcurrent::run, or run inside a separate QThread. Depending on the complexity of your code, you may be able to get away with the QtConcurrent method.

一些提示:您应该尽量保持主 (GUI) 线程的轻量。大量 IO 或计算应该使用 QtConcurrent::run 异步完成,或者在单独的 QThread 中运行。根据您的代码的复杂性,您或许可以使用 QtConcurrent 方法。

回答by timday

It's best to offload the long computations onto other threads so the main GUI thread remains responsive. The old-school uniprocessing way of doing things would be be to make sure your computations never run for too long without polling GUI event handler, but that doesn't scale to multi-cores.

最好将长计算卸载到其他线程上,以便主 GUI 线程保持响应。老式的单处理方式是确保您的计算不会在不轮询 GUI 事件处理程序的情况下运行太长时间,但这不会扩展到多核。

Fortunately Qt has excellent threading support. In the past you'd have to roll-you-own system for e.g farming out tasks to a thread-pool using QThread, QMutex, QWaitConditionetc, but recent Qt releases have made things easier with higher level abstractions like QThreadPool, QtConcurrent::runand QFuture.

幸运的是 Qt 具有出色的线程支持。在过去,你就得滚你,自己的系统,例如使用外包出去的任务的线程池QThreadQMutexQWaitCondition等,但最近发布的Qt做出的事情与更高层次的抽象,如更容易QThreadPoolQtConcurrent::runQFuture

回答by Macke

I don't know how things will go if you call QApplication::exec() from another thread, which then becomes your gui thread. Just an idea.

如果你从另一个线程调用 QApplication::exec() ,我不知道事情会怎样,然后它就变成了你的 gui 线程。只是一个想法。

(Let us know if it works, it'd be interesting...)

(让我们知道它是否有效,它会很有趣......)