Question about QTcpSocket and QThread



  • 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.



  • 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!!



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.