Multithreading with Qt (Part 3) - QThread with an event loop

preview_player
Показать описание
After a short presentation of a QThread without an event loop, this video will tell you about threads with an event loop. This allows handling events for objects in secondary threads, which is necessary in order to be able to call slots on these objects.

The video then presents two different ways to do this: creating both the thread and the worker at the same level, or encapsulating the thread into the worker.

The code is available on:

All Multithreading with Qt videos:

About the host:
This video is presented by David Faure, who started with Qt back in 1998 in the KDE project where he's still very active. This gives him 22 years of experience with Qt. Since 2003 he has taught many Qt training courses, on topics such as Qt Widgets, Multithreading, Modern C++ and Debugging/Profiling on Linux. He has made many contributions to QtCore, and has helped numerous customers, especially with Qt multithreading issues.

About KDAB:

KDAB experts regularly take time out to deliver KDAB’s world class training, in-house or at open enrollment courses around the world. We are the market leaders for training in Qt, OpenGL and C++.

Рекомендации по теме
Комментарии
Автор

Thanks a lot! Your patern where the QThread inside of the Worker saved me a lot of hours.

alekseyosipov
Автор

I am in a bit of lost here. what code in the worker does the m_thread->start() invoke to run?

yiren
Автор

Thanks for the video!
I tried the code of the pattern "active object".
From the main, I called deleteLater on the worker instance.
It seems that calling deleteLater means calling worker's destructor from the secondary thread.
Then it is drama! Worker destructor invokes cleanup method in the secondary thread, while trying to wait for the end of secondary thread... from the secondary thread (there is a warning message and I assume that the wait is purely ignored).
Then the destructor tries to delete the thread while it is not yet finished, and we have an error...
=> Does it mean that deleteLater should be banished with this kind of Workers (based on active object pattern)? Do I understand the full mechanism (more or less) correctly?
Thanks :)

pierrevueghs
Автор

Why is it necessary (in the "active object" approach) to execute quit() in the new thread, taking into account that this method is thread-safe?

juliangomez
Автор

Is there any benefit of using std::unique_ptr() instead of QScopedPointer() ?

Eugene-rqkr
Автор

at 4:43 why do we use "std::unique_ptr<QThread> m_thread" instead of "QThread * m_thread" ? And why do we use "m_thread.reset(new QThread);" instead of "m_thread = new QThread" ? is there an important difference?

mcflmnfs
Автор

At 6:15 there is talk about wait(), but my QT tells me: "QThread::wait: Thread tried to wait on itself" Can somebody help me what to do about it?

mcflmnfs
Автор

on slide at time 4:10 we are missing deleting of thread object when it is finished. We should add connect(thread, &QThread::finished, thead, &QThread::deleteLater);

vladimirkraus
Автор

Now, I see it. It starts the thread's event loop

yiren
Автор

can somebody translate what he says at 1:03 to English please? "A QObject is always a to a thread", I played the video at least 20 times, i cant understand what does he say. Can somebody please type this sentence? Thank you in advance.

mcflmnfs