[Development] Qthread::create and ::exec

Volker Hilsheimer volker.hilsheimer at qt.io
Wed Nov 21 15:47:58 CET 2018


On 20 Nov 2018, at 14:30, Иван Комиссаров <abbapoh at gmail.com> wrote:
> Hello, I've noticed that it would be nice to run QThread::exec() from a functor passed to QThread::create(). However, exec is marked as protected, thus I can't call QThread::currentThread()->exec().
> Maybe it is worth to make exec() public method? Or make it static, like in QApp (is it BC?)?
> This would be a nice addition to the create() method so worker objects can be created in a functor on stack instead of moving them to thread.
> 
> Иван Комиссаров


The reason why QThread::exec is protected so that people don’t get tempted to call it directly on a QThread object and expecting that the event loop is somehow executed in a separate thread. It is useful to generate a compile-time error when this is attempted:

QThread *heyNewThread = new QThread;
heyNewThread->exec();


I would rather not remove that limitation (and it would break BC on compilers that include the access level in the generated symbol). Making exec() public would indicate that the above is ok, while it actually breaks the semantics.

FWIW, if you reimplement run() you can allocate your objects on the stack and then call exec(). It’s of course some more boilerplate compared to using e.g a lambda as the worker.

Cheers,
Volker





More information about the Development mailing list