Thread issue



  • 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.



  • [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.



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



  • 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 ?



  • 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.



  • Yes, it is.



  • 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)
    {
    }@



  • 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.



  • 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.



  • [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]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.