[Qt-interest] QNetworkAccessManager & 2 threads

Andre Somers andre at familiesomers.nl
Fri Oct 15 14:55:35 CEST 2010


Op Vr, 15 oktober, 2010 8:29 am, schreef Thiago Macieira:
> On Friday 15. October 2010 07.46.26 Andre Somers wrote:
>> 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.
>
> Scott suggested passing the data to the other thread, not the
> QNetworkReply
> object. There's a very important difference there.
>
> QNR is created by QNAM and the two objects must be in the same thread as
> well,
> because of the sharing of internals (like the HTTP connection, cookie jar,
> etc.)
>
> However, the QByteArray data blocks, once read, can be safely passed to
> other
> threads.
>
Thank you for correcting me.

But do I understand you correctly that it would not even be safe to pass a
pointer to a QNetworkReply object after that object has emitted finished()
in order to read the data it has to offer? I thought that at that point,
QNAM is finished with it and it is up to the application to handle the
reply and delete it afterwards. If that is the case, then what is the
problem with passing that responsibility to another thread?

André





More information about the Qt-interest-old mailing list