QThreads General Usage

Threads in an operating system are a very simple thing. Write a function, maybe bundle it with some data and push it onto a newly created thread. Use a mutex or other method to safely communicate with the thread if necessary. Whether it are Win32, POSIX or other threads, they all basically work the same and are quite fool-proof. I’d venture to say that they’re at least a lot easier to use and handle than sockets :)

Those who have discovered the joys of the Qt framework may assume that threads in Qt (QThread) are just like this, but they would be both wrong and right. Wrong because years of wrong documentation from Trolltech/Nokia on QThread has caused countless people to use QThreads in a convoluted and highly inappropriate manner. Right because QThreads are in fact quite easy to use, as long as you ignored the Qt documentation that was in 4.8 and earlier.

Update: With the release of Qt 4.8.4, the official documentation [qt-project.org] shows this approach too

The main thing to keep in mind when using a QThread is that it’s not a thread. It’s a wrapper around a thread object. This wrapper provides the signals, slots and methods to easily use the thread object within a Qt project. This should immediately show why the recommended way of using QThreads in the documentation, namely to sub-class it and implement your own run() function, is very wrong. A QThread should be used much like a regular thread instance: prepare an object (QObject) class with all your desired functionality in it. Then create a new QThread instance, push the QObject onto it using moveToThread(QThread*) of the QObject instance and call start() on the QThread instance. That’s all. You set up the proper signal/slot connections to make it quit properly and such, and that’s all.

Declare Worker class

For a basic example, check this class declaration for the Worker class:

  1. class Worker : public QObject {
  2.     Q_OBJECT
  3.    
  4. public:
  5.     Worker();
  6.     ~Worker();
  7.    
  8. public slots:
  9.     void process();
  10.    
  11. signals:
  12.     void finished();
  13.     void error(QString err);
  14.    
  15. private:    
  16.     // add your variables here
  17. };

We add at least one public slot which will be used to trigger the instance and make it start processing data once the thread has started. Now, let’s see what the implementation for this basic class looks like.

  1. // --- CONSTRUCTOR ---
  2. Worker::Worker() {  
  3.     // you could copy data from constructor arguments to internal variables here.
  4. }
  5.  
  6.  
  7. // --- DESTRUCTOR---
  8. Worker::~Worker() {
  9.     // free resources
  10. }
  11.  
  12.  
  13. // --- PROCESS ---
  14. // Start processing data.
  15. void Worker::process() {
  16.  // allocate resources using new here
  17.  qDebug("Hello World!");
  18.  emit finished();
  19. }

While this Worker class doesn’t do anything special, it nevertheless contains all the required elements. It starts processing when its main function, in this case process(), is called and when it is done it emits the signal finished() which will then be used to trigger the shutdown of the QThread instance it is contained in.

By the way, one extremely important thing to note here is that you should NEVER allocate heap objects (using new) in the constructor of the QObject class as this allocation is then performed on the main thread and not on the new QThread instance, meaning that the newly created object is then owned by the main thread and not the QThread instance. This will make your code fail to work. Instead, allocate such resources in the main function slot such as process() in this case as when that is called the object will be on the new thread instance and thus it will own the resource.

Create a new Worker instance

Now, let’s see how to use this new construction by creating a new Worker instance and putting it on a QThread instance:

  1. QThread* thread = new QThread;
  2. Worker* worker = new Worker();
  3. worker->moveToThread(thread);
  4. connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
  5. connect(thread, SIGNAL(started()), worker, SLOT(process()));
  6. connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
  7. connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
  8. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
  9. thread->start();

The connect() series here is the most crucial part. The first connect() line hooks up the error message signal from the worker to an error processing function in the main thread. The second connects the thread’s started() signal to the processing() slot in the worker, causing it to start.

Then the clean-up: when the worker instance emits finished(), as we did in the example, it will signal the thread to quit, i.e. shut down. We then mark the worker instance using the same finished() signal for deletion. Finally, to prevent nasty crashes because the thread hasn’t fully shut down yet when it is deleted, we connect the finished() of the thread (not the worker!) to its own deleteLater() slot. This will cause the thread to be deleted only after it has fully shut down.

(taken from http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ [mayaposch.wordpress.com])

Categories: