August 6, 2012

henryxuv henryxuv
Lab Rat
42 posts

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.

2 replies

August 6, 2012

Jeroentje@home Jeroentje@ho..
Hobby Entomologist
232 posts

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

 Signature 

Greetz, Jeroen

August 7, 2012

franku franku
Hobby Entomologist
132 posts

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.

 Signature 

Keep in mind: This, Jen, is the internet.
.. frank

 
  ‹‹ How to initialize camera using Qt in Linux      Qt c++: how to check battery level (Laptop) ››

You must log in to post a reply. Not a member yet? Register here!