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. how to move multiple sockets to thread

how to move multiple sockets to thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 359 Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Mahi1990
    wrote on last edited by
    #1

    Re: how move to thread works

    @SGaist I trying to Subscriber for different systems each system should be on 1 socket. I am able to receive data for 1 subscriber like below:

    MainView.cpp:

        void MainView::startCommSubWorkerThread()
        {
            m_CommSubWorker.moveToThread(m_CommSubThread);
            connect( m_CommSubThread, &QThread::started, &m_CommSubWorker, &CommSubWorker::process);
            m_CommSubThread->start();
            m_CommSubThread->wait(100);
        }
    

    CommSubWorker.cpp:

    void CommSubWorker::process()
    {
    // Call to long running task
    m_CommSubFuel->run();

    emit finished();
    

    }

    CommSubFuel.cpp:

    void CommSubFuel::run()
    {
    qDebug() << "CommSubFuel::run()" << m_CommSubInit.getFuelSocket();
    runReceiveFuel(m_CommSubInit.getFuelSocket());
    }

    void CommSubFuel::runReceiveFuel(void *socket)
    {
    qDebug() << "runReceiveFuel->socket: " << socket;
    int nbOfBytesReceived = 0;
    zmq_msg_t part;
    memset(&part,0,sizeof(part));

    forever{
    // Receive messages until no more available
    do
    {
        qDebug() << "Size: " << sizeof(SystemFuel);
        zmq_msg_init_size(&part, sizeof(SystemFuel));
        // Check if a message is available to be received from socket
        nbOfBytesReceived = zmq_msg_recv(&part, socket, ZMQ_DONTWAIT);
        qDebug() << "In runReceive :" << nbOfBytesReceived ;
        if (nbOfBytesReceived > 0) {
            qDebug() << "In do loop";
            parseIncomingMessageFuel(zmq_msg_data(&part), nbOfBytesReceived);
        }
        zmq_msg_close(&part);
    
    } while (nbOfBytesReceived > 0);
    
    if (QThread::currentThread()->isInterruptionRequested())
        return;
    }
    

    }

    for subscriber thread will go into forever loop, can you please solution for me to for multiple system to receive data at a time on there own individual sockets. Is it required to create individual thread for each system?

    jsulmJ 1 Reply Last reply
    0
    • M Mahi1990

      Re: how move to thread works

      @SGaist I trying to Subscriber for different systems each system should be on 1 socket. I am able to receive data for 1 subscriber like below:

      MainView.cpp:

          void MainView::startCommSubWorkerThread()
          {
              m_CommSubWorker.moveToThread(m_CommSubThread);
              connect( m_CommSubThread, &QThread::started, &m_CommSubWorker, &CommSubWorker::process);
              m_CommSubThread->start();
              m_CommSubThread->wait(100);
          }
      

      CommSubWorker.cpp:

      void CommSubWorker::process()
      {
      // Call to long running task
      m_CommSubFuel->run();

      emit finished();
      

      }

      CommSubFuel.cpp:

      void CommSubFuel::run()
      {
      qDebug() << "CommSubFuel::run()" << m_CommSubInit.getFuelSocket();
      runReceiveFuel(m_CommSubInit.getFuelSocket());
      }

      void CommSubFuel::runReceiveFuel(void *socket)
      {
      qDebug() << "runReceiveFuel->socket: " << socket;
      int nbOfBytesReceived = 0;
      zmq_msg_t part;
      memset(&part,0,sizeof(part));

      forever{
      // Receive messages until no more available
      do
      {
          qDebug() << "Size: " << sizeof(SystemFuel);
          zmq_msg_init_size(&part, sizeof(SystemFuel));
          // Check if a message is available to be received from socket
          nbOfBytesReceived = zmq_msg_recv(&part, socket, ZMQ_DONTWAIT);
          qDebug() << "In runReceive :" << nbOfBytesReceived ;
          if (nbOfBytesReceived > 0) {
              qDebug() << "In do loop";
              parseIncomingMessageFuel(zmq_msg_data(&part), nbOfBytesReceived);
          }
          zmq_msg_close(&part);
      
      } while (nbOfBytesReceived > 0);
      
      if (QThread::currentThread()->isInterruptionRequested())
          return;
      }
      

      }

      for subscriber thread will go into forever loop, can you please solution for me to for multiple system to receive data at a time on there own individual sockets. Is it required to create individual thread for each system?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mahi1990 said in how to move multiple sockets to thread:

      m_CommSubThread->wait(100);

      Why do you wait for the thread?

      Why do you have an endless loop? Such loops block the Qt event loop!

      I suggest you take a look at this example: https://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        You are not using Qt sockets there since you are using ZMQ. You should take a look at this article from ICS implementing an application using ZMQ and Qt.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

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