Important: Please read the Qt Code of Conduct -

Qt QTcpSocket different threads for receive & transmit

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


  • 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:"

  • There's no need to use threading for I/O, as they are asynchronous (or non-blocking). (See also:

    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:

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


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

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

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

    call exec;

Log in to reply