Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt's default threads

Tags:

c++

macos

qt

When I start a new Qt application with a minimal amount of code and run it, I see that there are a number of threads running, which at a minimum is 2 and can be as many as 5. It usually settles at 2, until I drag the window around, at which time I see up to 4 threads running.

This is all the code I'm using: -

#include <QApplication>
#include <QWidget>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget mainWindow;
    mainWindow.show();

    return a.exec();
}

Can someone explain why there are different threads and what they're likely to be for? Originally I expected just one, but wouldn't be surprised if a second was used for handling messages. However, what could account for the other threads?

like image 487
TheDarkKnight Avatar asked Dec 16 '13 17:12

TheDarkKnight


People also ask

How many threads does Qt use?

As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes, for example QPixmap, don't work in secondary threads.

Is Qt multithreaded?

Qt offers many classes and functions for working with threads. Below are four different approaches that Qt programmers can use to implement multithreaded applications.

What is thread in Qt?

Detailed Description. A QThread object manages one thread of control within the program. QThreads begin executing in run(). By default, run() starts the event loop by calling exec() and runs a Qt event loop inside the thread. You can use worker objects by moving them to the thread using QObject::moveToThread().

How do you wait for QThread to finish?

Normally, with Qt you will have a QApplication based class with an event loop with signals and slots, that will not exit from the main function until you want to. In that case you can simply connect the QThread::finish() signal to a slot that checks if all threads are done.


1 Answers

I see now that you're asking out of curiosity in opposite to practical problems. Let's do some research.

I've tried to run your program on Qt 5.1 with MSVC toolkit on Windows. I configured the debugger to break on thread creation. I saw that 4 additional threads have been spawned. 3 of them were caused by Qt calling RegisterDragDrop native Windows function. When I skip QWindowsWindow::registerDropSite execution, these 3 threads are not created. There is no explanation about the threads even in the RegisterDragDrop documentation, not to say about Qt documentation. Obviously this fact can vary when using different OSs or Qt versions (for example you can build Qt without drag-n-drop support). The only way you can find out why the threads were created for you is an experiment. I think OS X also has some surprises for you.

The 4th thread is a mystery to me: debugger cannot detect when it is started. Maybe this thread is caused by debugger somehow.

As I expected, @tebe was wrong saying that Qt spawns additional threads for QTimer processing (I don't know about all cases, but in my case this is surely incorrect). QTimer uses the event loop of current thread.

like image 171
Pavel Strakhov Avatar answered Sep 21 '22 02:09

Pavel Strakhov