Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Question about QTcpSocket and QThread

Question about QTcpSocket and QThread

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    henryxuv
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

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

      Greetz, Jeroen

      1 Reply Last reply
      0
      • F Offline
        F Offline
        franku
        wrote on last edited by
        #3

        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.

        This, Jen, is the internet.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved