[Qt-interest] Best practices for a thread object
Arnold Krille
arnold at arnoldarts.de
Sun Jan 16 20:44:45 CET 2011
On Sunday 16 January 2011 19:56:53 you wrote:
> 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.
? Sean wrote about creating your object inside your QThread::run
implementation. He only lists moveToThread as the second option.
And all who answered your mail are non-qt-devs...
The approach with moveToThread only works because the default-implementation
of run() calls exec() to run an event-loop. But you will have better control
about your thread when you implement it yourself. And I personally don't trust
the moveToThread()-approach, to soon you will forget to which thread you moved
the object.
> 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 try to be constructive: Read about QtConcurrent.
It does exactly what you want, which is the traditional approach of thread-
implementations: Define a function that is executed in the new thread. And the
thread exits when that function is finished.
The advantage of QThread is that your thread is handled like an object and can
have signals/slots and an event-loop. But when you don't need that, you either
just implement your blocking operation in run() or you use the Worker from
QtConcurrent...
Have fun,
Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110116/8030a5cd/attachment.bin
More information about the Qt-interest-old
mailing list