[Interest] moveToThread used in constructor to move "this"

Thiago Macieira thiago.macieira at intel.com
Sat Feb 27 02:57:59 CET 2016


On quinta-feira, 25 de fevereiro de 2016 07:55:54 PST André Somers wrote:
> Op 24/02/2016 om 18:23 schreef Thiago Macieira:
> Would it be possible to circumvent this issue by moving the thread
> object onto its own thread too?
> 
>     Foo() : QObject(nullptr) {
>        moveToThread(&m_thread);
>        m_thread->moveToThread(m_thread);
>        m_thread.start();
>     }
> 
> 
> Looks like the much debated move-QThread-onto-its-own-thread
> antipattern, but it's not the same obviously.

No, that is exactly the "you're doing it wrong" pattern. It's just hidden by 
the fact that you moved it from outside the class, so you didn't use "this".

And like Prav said, this doesn't solve much. Foo's destructor must run inside 
that thread, so that thread hasn't exited. If the destructor tries to wait() 
for the thread to exit, it will deadlock.

The solution is to move the Foo object to another thread before destroying it.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Interest mailing list