Nominate our 2022 Qt Champions!

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…":, "Threads and QObject": and "Threads, Events and QObjects":

  • Don't forget to mention "the newly created wiki page": 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 ();
    connect (thread, SIGNAL(started()), this, SLOT(doSomeInit()));
    connect (this, SIGNAL(finished()), thread, SLOT(quit()));
    connect (this, SIGNAL(finished()), thread, SLOT(deleteLater()));

    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":
    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