[Qt-interest] QNetworkAccessManager & 2 threads

Andre Somers andre at familiesomers.nl
Fri Oct 15 07:46:26 CEST 2010


  Op 15-10-2010 3:00, Scott Aron Bloom schreef:
> I have gone with #2... Having a QNAM in each thread, and using the
> objects created from QNAM of each thread completely separate...
>
> Another option...
>
> Have the QNAM completely in the GUI, and when the data comes through
> send it to the thread for processing.
First of all: as far as I understand it - but please correct me if I am 
wrong - in Qt reentrant means that it is save to use the class from more 
than one thread at a time, provided *all* data related to it is private 
to that thread. That includes the this pointer, which is an implicit 
function argument in non-static class methods. That would imply you need 
separate instances. Again: I may be wrong on this one.

The second option Scott gives is not as unsafe as it may sound, I think. 
If you want, you could have your main thread do the network handling, 
and only pass the QNetworkReply object pointers to the handling thread 
after they have signalled their finished() signal. At that point, 
QNetworkReply will not be accessed anymore by QNAM, so you can just have 
your second thread access it and read its data, as long as your own code 
for the main thread doesn't touch the reply anymore.
On the other hand: it would IMHO also make sense to have the QNAM in 
your tread. If you do heavy processing on the data, you need to make 
sure the eventloop in that thread actually gets to run properly, but you 
have the same issue in the main thread where you do heavy graphics work. 
Would it be possible to do that heavy graphics work in a separate thread 
by rendering into a QImage?

André





More information about the Qt-interest-old mailing list