I have a simple multithreading application that works like the example "Threaded Fortune Server Example" on Qt website.
When my QTCPServer receive an incoming connection, I create a QRunnable task, passing the socketDescritptor and then submitting it to a QThreadPool:
void Server::incomingConnection(qintptr socketDescriptor)
{
Task *task = new Task(socketDescriptor, this);
this->m_pool->start(task);
}
then in the Task method run()
I create the socket:
void Task::run()
{
QTcpSocket tcpSocket;
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket.error());
return;
}
...
}
However, I read those note about incomingConnection method of QTCPServer from the Qt Doc
Note: If another socket is created in the reimplementation of this method, it needs to be added to the Pending Connections mechanism by calling addPendingConnection().
Note: If you want to handle an incoming connection as a new QTcpSocket object in another thread you have to pass the socketDescriptor to the other thread and create the QTcpSocket object there and use its setSocketDescriptor() method.
So, my questions are:
- how can I add the socket created from another thread to the pending connection of the Server?
- is this a needful operation?
Thanks,