Qt World Summit: Submit your Presentation

QT multi-thread socket quit issue

  • As title, I made a project to use multi-thread realize multi-socket client connection.
    I choose 'move to a thread' to make my data processing in the new thread.
    I test it works, "thread over " can be print
    My question is are there any risks, or how can I know the tread is over?

    void Server::incomingConnection(qintptr socketDescriptor)
        qDebug()<<"incoming in thread: "<<QThread::currentThreadId();
        Thread *thread = new Thread(socketDescriptor,this);
        Worker *cpu=new Worker(thread->socket);
        connect(thread->socket, SIGNAL(disconnected()), cpu, SLOT(socket_Disconnected()));
        connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
        connect(thread, SIGNAL(finished()), this, SLOT(Over()));
        cpu->moveToThread (thread);
    void Server::Over()
        qDebug()<<"thread over!";

    in server.cpp I overwriter the incommingconnection function, and connect 2 slot and signal. I want to realize that when socket is disconnected, the thread should be quit and release all the resource.

    this is worker.cpp

    void Worker::socket_Read_Data()
    //data processing
    void Worker::socket_Disconnected()

  • Lifetime Qt Champion

    This doesn't work at all. QThread is not a thread but an object which creates a thread. All stuff inside QThread::run() is running in another thread. Therefore when you create your QTcpSocket inside Thread's ctor it will be created in the main thread.
    The easiest way to go is

    QThread *t = new QThread(this);
    Worker *w = new Worker(socketDescriptor); // no parent - otherwise moveToThread will not work, see documentation!
    connect(t, &QThread::started, w, &Worker::doWork);

    The Worker is started in the

  • tks a lot.
    have you finished the answer? I find the last sentences is ‘the worker is started in the’

    by the way, how can I finish the thread when the socket is disconnected.

    tks again.

  • @gaosiy

    Assuming that Worker has Socket instead of is Socket (relationship).
    You can forward the socket disconnected signal to a worker signal to stop the thread

     // in worker and thread creation
     connect(t, &QThread::finished, w, &Worker::deleteLater);
     connect(t, &QThread::finished, t, &QThread::deleteLater);
     connect(w, &Worker::socketDisconnected, t, &QThread::quit);
    // in worker constructor
    connect(mySocket, &QTcpSocket::disconnected, this, &Worker::socketDisconnected); // worker signal

    Or use a slot function from your worker to stop its thread.

    // in worker and thread creation
     connect(t, &QThread::finished, w, &Worker::deleteLater);
     connect(t, &QThread::finished, t, &QThread::deleteLater);
    // in worker constructor
    connect(mySocket, &QTcpSocket::disconnected, this, &Worker::onSocketDisconnected); // worker slot
    // worker onSocketDisconnected Slot function
    void Worker::onSocketDisconnected()

  • @KillerSmath
    tks, it all works, so do my code above.
    I think I find the original question, that is QTcpSocket::disconnected cant be emitted in some cases.
    When I use software to connect my server and disconnect it, QTcpSocket::disconnected can be emitted.
    When I use my two network hardware to connect, one of them cant make the QTcpSocket::disconnected work when I shut down the hardware, another one can make the QTcpSocket::disconnected work when I shut down, but it works after 10 seconds, it's very strange and confused me.
    I will find why and how to check the connection state and use another way to deal with the disconnection issue.

    tks again KillerSmath, you are so warmhearted!

Log in to reply