[Qt-interest] Best practices for a thread object
Andre Somers
andre at familiesomers.nl
Sun Jan 16 21:02:43 CET 2011
Hi Etienne,
Thanks for the interesting question.
Op 16-1-2011 19:56, Etienne Sandré-Chardonnal schreef:
> Thanks from the beginner. Have we met before?
>
> The 4.7.1 docs says "To create your own threads, subclass QThread and
> reimplement run <http://doc.qt.nokia.com/latest/qthread.html#run>()".
> Apparently (since the first post I have read many debates on the
> topic) subclassing is definitely not the method recommended by the qt
> developpers... Thus, the doc needs refreshing.
I agree, they *do* need refreshing. Not that the current description is
wrong per sé, they are just not clear on the now recommended way of
doing things. I know they are not meant to give a complete introduction
to multi-threaded programming, but they may at least mention that if you
want to use signals and slots between threads, the easiest way to reach
that is *not* to subclass QThread, but to just create a QObject
subclass, move it to a new vanilla QThread, and call start() on it.
> Second question to the constructive people : what about threads that
> will do nothing but pure blocking computations in a loop? The approach
> described above is based on the event loop. Would you still keep that
> approach and put the blocking computations in a slot? There is no real
> sense to start an event loop in that case.
> For the non-blocking threads that do some I/O for instance, I like
> that non-subclassing approach, thanks.
I think subclassing QThread, and reimplementing run() to process a bunch
of data or perform some longish calculation is a perfectly fine
approach. Nothing wrong with that, IMHO, and indeed: no need for an
eventloop. That also means no signals and slots*. Just let the run()
function end, and wait for the QThread::finished() signal.
However, if you have a paralizable problem, then I would also recommend
looking into QtConcurrent.
André
* signals are technically possible, but I would not recommend it...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110116/574fbf3f/attachment.html
More information about the Qt-interest-old
mailing list