Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Thread issue
QtWS25 Last Chance

Thread issue

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    f.vanalmetelevic.com
    wrote on last edited by
    #1

    I have a class, say MyClient, that inherits from QTcpSocket. Within the class constructor, I create a thread, say MyThread, that inherits from QThread. The readyRead signal of MyClient is linked to a slot of MyThread. The slot contains a while loop that reads small parts of the available data. For test (to see if thread was working as expected...), I added a sleep(5) within the while loop. Result : whenever the thread waits for 5 seconds in the sleep, the whole GUI is blocked. I would have expected that only the thread would block and all the rest would continue normally. What am I doing wrong here ?

    update : QTcpSocket is created from the main thread and I want to handle the incoming data in a separate thread.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      [quote author="filip" date="1331124703"]What am I doing wrong here ?[/quote]
      You haven't read "You’re doing it wrong…":http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/, "Threads and QObject":http://qt-project.org/doc/qt-4.8/threads-qobject.html and "Threads, Events and QObjects":http://qt-project.org/wiki/Threads_Events_QObjects.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KA51O
        wrote on last edited by
        #3

        Don't forget to mention "the newly created wiki page":http://qt-project.org/wiki/QThreads_general_usage on this topic Lukas ^^

        1 Reply Last reply
        0
        • F Offline
          F Offline
          f.vanalmetelevic.com
          wrote on last edited by
          #4

          Thanks for the links !

          In the examples, an object is created, the object is moved to the thread and then the thread is started.
          Is it possible to assign other objects to that thread AFTER it is started ?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            Yes, that is possible. You can push an object to any thread at any time.
            Signals and slots will be handled, unless the eventloop of that thread is running.

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #6

              Yes, it is.

              1 Reply Last reply
              0
              • F Offline
                F Offline
                f.vanalmetelevic.com
                wrote on last edited by
                #7

                I guess this is also possible then ?

                @MyClass::MyClass (QObject *parent) : QObject(parent)
                {
                QThread *thread = new Thread ();
                this->moveToThread(thread);
                connect (thread, SIGNAL(started()), this, SLOT(doSomeInit()));
                connect (this, SIGNAL(finished()), thread, SLOT(quit()));
                connect (this, SIGNAL(finished()), thread, SLOT(deleteLater()));
                thread->start();
                }

                void MyClass::doSomeInit (void)
                {
                }@

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  This could lead to interesting behavior:

                  @
                  connect (this, SIGNAL(finished()), thread, SLOT(deleteLater()));
                  @

                  you don't know, which clot will be called first, the quit from the thread or the deleteLater. deleteLater MUST be executed in the message loop of the object (which is the one of the thread). quit the thread will stop the message loop.

                  By MyClass is created from another Thread, also the Thread itself belongs to the creating thread.

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    f.vanalmetelevic.com
                    wrote on last edited by
                    #9

                    Should be better like this, I think :

                    @ connect (thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
                    ^^^^^^
                    @

                    finished of MyClass will make the thread stop. Thread will emit finished when it stops, which will delete the thread object.

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lgeyer
                      wrote on last edited by
                      #10

                      [quote author="Gerolf" date="1331201250"]you don't know, which clot will be called first, the quit from the thread or the deleteLater.[/quote]

                      As far as I remember this has changed with 4.7. The execution of multiple slots is now guaranteed to be in the order of the connect statements.

                      [quote]"Signals and Slots":http://qt-project.org/doc/qt-4.8/signalsandslots.html
                      If several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted.[/quote]

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved