[Development] What kind of airplane we want to build?
Thiago Macieira
thiago.macieira at intel.com
Thu Jan 21 02:02:11 CET 2016
On Thursday 21 January 2016 02:37:43 Marc Mutz wrote:
> On Wednesday 20 January 2016 21:52:49 Thiago Macieira wrote:
> > On Wednesday 20 January 2016 21:08:07 Pau Garcia i Quiles wrote:
> > > Replacing QThread with std::thread? Please don't.
> >
> > Unlike the containers or even futures/promises vs QtConcurrent, we can
> > easily argue that QThread provides a lot more functionality than
> > std::thread. In just one word: signals.
>
> What happened to "you're doing it wrong!"? :) AFAIU that blog post, you're
> supposed to use the signals of QObjects living in that thread, not the
> QThread itself.
You can use the signals and the slots of the QThread itself. The wrong part
was deriving from QThread, adding slots to it and then doing
moveToThread(this) so those slots got called in the thread that it manages.
Other uses of QThread are fine. You can derive from it and override run. You
can even add more signals to it. It's new slots that are suspicious.
> And that's perfectly possible with std::thread, too:
>
> auto po = new ProcessingObject();
> connect(po, ...);
> po->moveToThread(nullptr); // enable "pulling" moveToThread()
> auto t = std::thread([po, gui = QThread::currentThread()]() {
> QEventLoop loop;
> po->moveToThread(QThread::currentThread());
> connect(po, ..., &loop, &QEventLoop::quit);
> loop.exec();
> po->moveToThread(gui);
> // or: delete po;
> }
>
> Am I missing something?
How do you interrupt the event loop from outside (QThread::quit)? You can't
create that QEventLoop before the lambda because it would live in the wrong
thread. You could use this:
po->thread()->quit();
but note how you used QThread. If you're going to use QThread anyway, you may
as well use it to start the thread.
Even if you didn't do this, your example uses QThread
(QThread::currentThread(), which returns a QAdoptedThread pointer). And even
if you didn't moveToThread(), you created a QObject in that thread (the
QEventLoop), which will create the QThreadData and the event dispatcher
machinery.
At that point, QThread is a simple wrapper full of convenience functions.
> > We should provide QThread::makeThread() taking a lambda and some other
> > niceties, though.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
More information about the Development
mailing list