I am creating a small multi-threaded web server. QTcpSockets are retrieved in the main thread and then passed to QtConcurrent to QThreadPool, which ultimately processes the data and sends a response.
My problem is that the socket is created in the main thread and processed in another. This causes errors when trying to write to the socket:
socket->write(somedata);
QObject: It is not possible to create children for a parent that is in another thread. (Parent of QNativeSocketEngine (0x608330), parent thread is QThread (0x600630), current thread is QThread (0x505f60)
A clean way would be to move the socket object into the processing flow using
socket->moveToThread(QThread::currentThread()).
However, this can only be called on the thread in which the object was created. In addition, the socket has the QTcpServer object as the parent, so moveToThread () will still fail (parent objects cannot switch threads).
How can I move an object to QThread :: currentThread () in the code that threadpool starts? Alternatively, how can I write to a socket outside the stream that was created?
multithreading qt qt4
grefab
source share