[Qt-interest] QThread event loop running in main thread?

Mihail Naydenov mlists at ymail.com
Tue Dec 28 11:52:50 CET 2010


With the risk of being ashamed I must admit I do use the wrong way :)

I still am not sold why should I not be doing it. I find the reasons against it 
to academic - "Do the work in a object, devoted to that, and let a specific 
class handle the threading part - encapsulation and specialization".
Well, merging the two is however to damn convenient in practice.

The scenario I use (inspired by QFileSystemWatcher ) is to have Worker and 
WorkerEngine.

Worker is simple interface to the world (signals, slots, functions), manages a 
private WorkerEngine.
WorkerEngine is qthread and is lives on that thread.

Worker queues work for WorkerEngine be invoking its methods, also handles 
cancellation by calling its cancel/quit functions. 

WorkerEngine does all the work in Q_INVOCABLE functions, also sends results by 
invoking methods (in most cases the actual public result-signals), on the 
Worker.

Yes the design is "bad" because of the tight coupling of two, even three 
classes, but to the world it is one - Worker, all other mess is impl. detail.
The important part however is - it can not be made simpler than that to have 
threaded work done. 

Both reimpl run() and having separate qthread object to carry WorkerEngine 
introduces some (uneeded) addition management.
In first case - to have a manual work queue;
In the second - to have stubs for communicating with qthread, you lose sleep(), 
have (yet) another QObject allocation and management. Yes these are all trival, 
but as I said - unnecessary, you gain nothing (at least I don't). 


So in the end the scenario is by no means perfect, but cant be more simpler.

Thanks
MihailNaydenov


----- Original Message ----
> From: Thiago Macieira <thiago at kde.org>
> To: qt-interest at qt.nokia.com
> Sent: Tue, December 28, 2010 12:28:59 AM
> Subject: Re: [Qt-interest] QThread event loop running in main thread?
> 
> On Monday, 27 de December de 2010 18:00:44 Hannu Shemeikka wrote:
> > Maybe  you guys, Nokia or whoever is responsible, should update the 
> > documents.  Especially the QThread-stuff. It's written there that "to 
> > create your  own threads, subclass QThread and reimplement run()."
> 
> Why?
> 
> That is  not wrong. It is accurate, even if not complete.
> 
> Like Mihail said further  down the thread, the wrong thing to do is to have 
> slots in the class derived  from QThread and/or using moveToThread(this).
> 
> If you treat the QThread  object like I said, as the object managing an 
> operating system resource (the  thread), not the work to be done, you're fine.
> 
> -- 
> Thiago Macieira -  thiago (AT) macieira.info -  thiago (AT) kde.org
>    Senior Product Manager - Nokia, Qt Development Frameworks
>        PGP/GPG: 0x6EF45358; fingerprint:
>       E067 918B B660  DBD1 105C  966C 33F5 F005 6EF4 5358
> 


      



More information about the Qt-interest-old mailing list