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. QT multi-thread socket quit issue
QtWS25 Last Chance

QT multi-thread socket quit issue

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.1k 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.
  • gaosiyG Offline
    gaosiyG Offline
    gaosiy
    wrote on last edited by
    #1

    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?

    //server.cpp
    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(readyRead()),cpu,SLOT(socket_Read_Data()));
        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);
        cpu->socket->moveToThread(thread);
        thread->start();
    }
    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

    //worker.cpp
    void Worker::socket_Read_Data()
    {
    //data processing
    }
    
    void Worker::socket_Disconnected()
    {
    
       this->thread()->quit();
       this->thread()->wait();
    
    }
    
    

    Biomedical Engineering Ph.D. student, China

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      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);
      connect(...)
      w->moveToThread(t);
      t->start();
      

      The Worker is started in the

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      5
      • gaosiyG Offline
        gaosiyG Offline
        gaosiy
        wrote on last edited by
        #3

        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.

        Biomedical Engineering Ph.D. student, China

        KillerSmathK 1 Reply Last reply
        0
        • gaosiyG gaosiy

          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.

          KillerSmathK Offline
          KillerSmathK Offline
          KillerSmath
          wrote on last edited by KillerSmath
          #4

          @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()
          {
              this->thread()->quit();
          }
          

          @Computer Science Student - Brazil
          Web Developer and Researcher
          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

          gaosiyG 1 Reply Last reply
          4
          • KillerSmathK KillerSmath

            @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()
            {
                this->thread()->quit();
            }
            
            gaosiyG Offline
            gaosiyG Offline
            gaosiy
            wrote on last edited by
            #5

            @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!

            Biomedical Engineering Ph.D. student, China

            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