Results 1 to 4 of 4

Thread: QTcpSocket - write to socket from several threads

  1. #1
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Question QTcpSocket - write to socket from several threads

    I am writing a client server application using QTcpSocket.

    Two questions:

    1. My application is multi-threaded and several threads may call to socket.write() method.
    Should I protect calling to write method (using Mutex) or QIODevice::write is MT safe ?

    2. If we read and write to the same socket , do we need to lock in application level or does TCPSocket already handles that ?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket - write to socket from several threads

    QTcpSocket does not provide any locking itself, that would be wasteful in single threaded usage cases (which is almost always the case).

    If I were you I would really avoid accessing the socket from multiple threads.
    For example if your threads assemble messages that need to be sent, let them emit the message in a signal and let the thread owning the socket send them.

    Cheers,
    _

  3. #3
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTcpSocket - write to socket from several threads

    Thanks a lot for your reply.

    I absoultly agree that writing to the socket should be done from a single thread.

    What about reading and writing to the sokcet from different threads ? One thread read while the other thread write ?

    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket - write to socket from several threads

    Quote Originally Posted by Einat View Post
    What about reading and writing to the sokcet from different threads ? One thread read while the other thread write ?
    There is really no reason to do that. Reading from a socket is not a time consuming operation - you can read from the socket and emit a signal with the data read. The signal can be caught from any thread you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    Einat (10th November 2013)

Similar Threads

  1. QTCP Socket Server and threads
    By cafu1007 in forum Qt Programming
    Replies: 21
    Last Post: 2nd November 2012, 16:00
  2. why can't i write some string to the socket
    By succulent_lily in forum Qt Programming
    Replies: 5
    Last Post: 3rd December 2008, 09:26
  3. threads and socket
    By babu198649 in forum Newbie
    Replies: 3
    Last Post: 10th April 2008, 15:34
  4. question about socket and threads?
    By oob2 in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2007, 11:42
  5. How to write on a socket in another thread?
    By Valheru in forum Qt Programming
    Replies: 7
    Last Post: 12th October 2006, 10:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.