Results 1 to 11 of 11

Thread: I think, there is a problem with QNetworkReply and threads

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2006
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: I think, there is a problem with QNetworkReply and threads

    However. I will drop this code totally and write my own class to access the server.

    The other side (the server) is an Intel sa1110 Chip with 206 MHz (a Mobotix webcam) and my client runs on some Opteron 244 with 1.8GHZ. The Opteron needs 100% CPU-Time just for pure reading. This is far too slow code.

    BTW: It works okay, if I create the three classes in main() and just do the retrieving in the subthread.

  2. #2
    Join Date
    Mar 2006
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 7 Times in 6 Posts

    Default Re: I think, there is a problem with QNetworkReply and threads

    In first post example _reply object is read/written in h object thread (run method...) and read/written in main thread (dataAvailable slot) at same time (without synchronization. For example some pointer became invalid while still used as valid in other thread).
    The Opteron needs 100% CPU-Time just for pure reading.
    Try to wait until 100000 or more bytes arrive and then read them. The finished signal to get the last portion of bytes.
    Try (in this case Hurtz slots will be executed in Hurtz thread)
    Qt Code:
    1. Hurz::Hurz()
    2. {
    3. moveToThread(this);
    4. }
    To copy to clipboard, switch view to plain text mode 
    Some example (it crashed before using moveToThread)
    Qt Code:
    1. #include <QNetworkAccessManager>
    2. #include <QNetworkReply>
    3. #include <QCoreApplication>
    4. #include <QThread>
    5. #include <QEventLoop>
    6. #include <QDebug>
    7. #include <QTime>
    8.  
    9. class Hurz : public QThread
    10. {
    11. Q_OBJECT
    12.  
    13. public slots:
    14. void dataAvailable();
    15. void slot_finished();
    16.  
    17. public:
    18. Hurz();
    19. void run();
    20.  
    21. private:
    22. QNetworkAccessManager *_manager;
    23. QNetworkReply *_reply;
    24. QNetworkRequest *_request;
    25. QTime m_time;
    26. };
    27.  
    28. Hurz::Hurz()
    29. {
    30. moveToThread(this);
    31. m_time = QTime::currentTime();
    32. }
    33.  
    34. void Hurz::run()
    35. {
    36. qDebug() << "Hurz::run thread id is " << QThread::currentThreadId();
    37. _manager = new QNetworkAccessManager();
    38.  
    39. // you need to change the url to a different one which sends you "endless" data, eg. a file with almost gigabytes of data
    40. _request = new QNetworkRequest(QUrl("http://ftp.stack.nl/pub/users/dimitri/doxygen-1.6.2-setup.exe"));
    41. _reply = _manager->get(*_request);
    42. connect(_reply, SIGNAL(readyRead()), this, SLOT(dataAvailable()));
    43. connect(_reply, SIGNAL(finished()), this, SLOT(slot_finished()));
    44. exec();
    45. }
    46.  
    47. void Hurz::dataAvailable()
    48. {
    49. if (_reply->size() > 100000) {
    50. qDebug() << "Hurz::dataAvailable: " << "Thread id is" << QThread::currentThreadId();
    51. QByteArray arr = _reply->readAll();
    52. int ms = m_time.msecsTo(QTime::currentTime());
    53. m_time = QTime::currentTime();
    54. qDebug() << " " << arr.size() << "in" << ms << "ms. " << (float)arr.size() / 1024 / ms * 1000 << "kB/s";
    55. }
    56. }
    57.  
    58. void Hurz::slot_finished()
    59. {
    60. qDebug() << "Hurz::slot_finished: " << "Thread id is" << QThread::currentThreadId();
    61. if (_reply->size() > 0) {
    62. QByteArray arr = _reply->readAll();
    63. int ms = m_time.msecsTo(QTime::currentTime());
    64. m_time = QTime::currentTime();
    65. qDebug() << " " << arr.size() << "in" << ms << "ms. " << (float)arr.size() / 1024 / ms * 1000 << "kB/s";
    66. }
    67. quit();
    68. }
    69.  
    70. int main(int argc, char **argv)
    71. {
    72. QCoreApplication app(argc, argv);
    73.  
    74. Hurz h;
    75. //QEventLoop evLoop;
    76. QObject::connect(&h, SIGNAL(finished()), &app, SLOT(quit()));
    77. h.start();
    78.  
    79. //for(;;)
    80. // evLoop.processEvents(QEventLoop::ExcludeUserInputEvents);
    81. //evLoop.exec(QEventLoop::ExcludeUserInputEvents);
    82. app.exec();
    83. }
    84.  
    85. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Rembobo for this useful post:

    Wurgl (12th January 2010)

  4. #3
    Join Date
    Apr 2006
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: I think, there is a problem with QNetworkReply and threads

    Thanks!

    moveToThread() was the solution for both the crash and the time-eating. My selfwritten class uses about 2-3% of CPU, Qt uses about 7-8% which is okay.

    However, it seems to be a little bit strange to me, I really expected, that a subclass of QThread already lives in that thread.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: I think, there is a problem with QNetworkReply and threads

    QThread object is the thread controller, not the thread itself. And if QThread lived in the thread it represents, what would happen to it once the thread was finished?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Apr 2006
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: I think, there is a problem with QNetworkReply and threads

    Quote Originally Posted by wysota View Post
    What do you need threads here for anyway?
    Reason 1: The example ist just a dummy code. The real program has multiple stages with image processing, each of these stages may be time consuming. And since I get images in an endless stream, I avoid loosing any of these images. Each stage shall run in its own thread, stage 1 is grabbing the images, stage 2 is some analysing and filtering, stage 3 is some neuronal network processing, stage 4 is storing in the file system ... and a few more stages
    Reason 2: Finally it will run on a machine with 8 cores, so why not using all of them?
    Reason 3: Because it is cool, when *ix-utility top shows a cpu-usage greater than 100% :-)

    Quote Originally Posted by wysota View Post
    QThread object is the thread controller, not the thread itself. And if QThread lived in the thread it represents, what would happen to it once the thread was finished?
    So a clean implementation is to create some subclasses of QObjects in QThread::run() which do the real work and not using moveToThread()?

  7. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 140 Times in 132 Posts

    Default Re: I think, there is a problem with QNetworkReply and threads

    Quote Originally Posted by Wurgl View Post
    However, it seems to be a little bit strange to me, I really expected, that a subclass of QThread already lives in that thread.
    Because you haven't read the Qt Documentation about QThread class :P where it is clearly written than body of run() is finally in new created thread with QThread::start() method.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. Problem with threads
    By kvnlinux in forum Newbie
    Replies: 7
    Last Post: 21st August 2009, 17:29
  2. When to delete QNetworkReply?
    By QPlace in forum Qt Programming
    Replies: 5
    Last Post: 11th February 2009, 12:46
  3. Problem creating a QWidget using threads
    By tarod in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2007, 12:45
  4. Threads problem
    By boss_bhat in forum Qt Programming
    Replies: 3
    Last Post: 2nd August 2006, 15:41
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.