C++/Qt - QThread vs QRunnable

2020-02-04 11:41发布

问题:

What are the differences between QThreads and QRunnable ?

When should I use QThread and when QRunnable ?

回答1:

The QRunnable class and the QtConcurrent::run() function are well suited to situations where we want to perform some background processing in one or more secondary threads without needing the full power and flexibility provided by QThread.

from "Advanced Qt Programming: Creating Great Software with C++ and Qt 4" by Mark Summerfield



回答2:

QThread can run an event loop, QRunnable doesn't have one so don't use it for tasks designed to have an event loop. Also, not being a QObject, QRunnable has no built-in means of explicitly communicating something to other components; you have to code that by hand, using low-level threading primitives (like a mutex-guarded queue for collecting results, etc.). Using QThread you can use signals and slots which are thread safe.