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