[Qt-interest] QThread Question

Arnold Krille arnold at arnoldarts.de
Thu Aug 6 14:03:06 CEST 2009


Hi,

On Thursday 06 August 2009 12:43:01 Donal O'Connor wrote:
> I've got code here written by somebody else that seems to make use of
> several threads but I'm quite worried that the processing is not done in a
> different thread like intended.
> They've created an object derieved from QThread but never implemented the
> run method so it defaults to the exec() event state when started.
> Now, they've created slots and these slots by the looks of it does the hard
> work which to the best of my knowledge results them being run in the
> calling thread.

Yep, these slots will be executed in the thread that created the QThread-
derived object (lets call it T). If they should be executed in the new thread, 
these slots would have to be slots off an object created inside T's run-
function and thus living in the new thread. And the slots would have to be 
called by queued connections.

> Am I correct in saying, that if you call a method of a QThread object or
> arrive at a slot due to a Queued connection that the processing done in the
> slot/method
> is done in the calling thread

Yes.

> and not in the actual thread that was stared
> for the QThread object?

The new thread is not started _for_ the QThread, it is started _inside_ the 
QThreads run()-function.

> To the best of my knowledge, the way it should be done, is all the
> processing done in the run() method but use the methods/slots to change
> data/state of thread of the thread but using Mutex's since it's the calling
> thread that will be modifying the data and should be mutually exclusive
> from the run method which reads or writes to this data.

Using mutexes (or ringbuffers) you could feed the needed data into the new 
thread from the slots of the QThread-object, yes.

Have fun,

Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090806/db587ce5/attachment.bin 


More information about the Qt-interest-old mailing list