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. Qt QTcpSocket different threads for receive & transmit
Forum Updated to NodeBB v4.3 + New Features

Qt QTcpSocket different threads for receive & transmit

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 10.1k 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.
  • R Offline
    R Offline
    rkintada
    wrote on last edited by
    #1

    Hi,

    I am writing a simple client server tool, where the server streams the data when available to the clients connected to the server. The thread which generates the server data( to be transmitted) is different from the one that created the Client socket. If i call the write function on the socket in the data generator thread context, i get an error. How can I fix this? looks like from all the documentation, socket's read and write should happen the same thread context, which does not seem to make sense, as they both can be independent. Is there some work around where I can use a mutex instead and have write operations from different thread contexts.

    Thanks
    Ramak

    1 Reply Last reply
    0
    • G Offline
      G Offline
      GordonSchumacher
      wrote on last edited by
      #2

      A small poke at the Doctrolls... the information you want is actually under QIODevice:

      bq. "Certain subclasses of QIODevice, such as QTcpSocket and QProcess, are asynchronous. This means that I/O functions such as write() or read() always return immediately, while communication with the device itself may happen when control goes back to the event loop. QIODevice provides functions that allow you to force these operations to be performed immediately, while blocking the calling thread and without entering the event loop. This allows QIODevice subclasses to be used without an event loop, or in a separate thread:"

      1 Reply Last reply
      0
      • W Offline
        W Offline
        w00t
        wrote on last edited by
        #3

        There's no need to use threading for I/O, as they are asynchronous (or non-blocking). (See also: http://en.wikipedia.org/wiki/Asynchronous_I/O)

        In general, threading is used in a lot of cases where it probably shouldn't be used.

        For information on when to use (and not to use) threads, see these resources:

        • http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects
        • http://blog.exys.org/entries/2010/thread_abuse.html

        --
        http://rburchell.com

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rkintada
          wrote on last edited by
          #4

          That's all true. Here is a sample pseudo(reduced version) of what I am trying to do:
          for( step = 0; step < n; step++ )
          Get Input // either from socket/file
          RunAlgorithm // this is compute intensive and can take time.
          WriteOutput // the intent is to run a QTcpServer and write the output to all the connected
          // clients.
          call exec;

          So if "exec" is not called till the end, then event loop does not run and no output will be sent( if the TCPServer is created in the main thread context). Hence I created a QTcpServer in a thread. In this case, the WriteOutput fails as I am trying to access a socket created in TcpServer thread context( in the newConnection handler ). Probabily i could use events and signals and get rid of the for loop. However i would like to do it without changing the design. Hence the question.

          Thanks
          RamaK

          1 Reply Last reply
          0
          • W Offline
            W Offline
            w00t
            wrote on last edited by
            #5

            Well, to provide a simple answer then, yes, you'll need to modify your design. Qt's IO isn't designed for use on multiple threads.

            It should be pretty easy to communicate with a QThread-based solution performing your algorithm from your main thread where the sockets are doing the reading/writing.

            --
            http:&#x2F;&#x2F;rburchell.com

            1 Reply Last reply
            0
            • R Offline
              R Offline
              rkintada
              wrote on last edited by
              #6

              Resending the pseudo code as it was not formatted correctly earlier.

              @for( step = 0; step < n; step++ )
              {
              // either from socket/file
              Get Input

              // this is compute intensive and can take time.
              RunAlgorithm

              // the intent is to run a QTcpServer and write the output to all the connected clients.
              WriteOutput

              }
              call exec;
              @

              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