Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Question about QTcpSocket and QThread
henryxuv last edited by
When I trying to use a QTcpSocket in a multithread enviroment, what should I pay attention to? As I saw, the QTcpSocket is not thread-safe.
Jeroentjehome last edited by
Because the QTcpSocket is reentrant you only need to take good care of how to handle the data send or received from the socket. If more then 1 thread uses the same data you will not be able to use the QList etc to handle this because that class is not thread safe. You might have to look into QMutex class to handle data acces between threads.
Hope this give a bit of direction. Have fun coding!!
franku last edited by
You have to keep track which thread will close the socket since only the creator-thread of a socket is allowed to close a socket.
A safer way would be to distribute only the socket descriptor and have each thread its own QTcpSocket that uses setSocketDescriptor to use the communication peer.