Results 1 to 2 of 2

Thread: Threaded echo server - confused about signals/slots within a thread

  1. #1
    Join Date
    Sep 2013
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Threaded echo server - confused about signals/slots within a thread

    I used the Threaded Fortune Server as a base, and created a threaded echo server. While it compiles, it has a problem that I think is very basic/foundational. (It crashes when I write to the socket with a message about creating a thread for a different parent, but I won't get into that here).

    In my single thread I connect a readyRead signal from the tcpsocket to a readCommand slot inside the same thread. From what I've read, you should not have a signal from inside a thread connect to a slot in the same thread. Is that right?

    If that's the problem, how should I design this server? Do I have to create 2 threads for each connection? (One for when the incoming connection is established, and a second to read/write from that connection?) And how would I get the tcpSocket info passed from one thread to the other. Confused!!

    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: Threaded echo server - confused about signals/slots within a thread

    Quote Originally Posted by ocgltd View Post
    I used the Threaded Fortune Server as a base, and created a threaded echo server. While it compiles, it has a problem that I think is very basic/foundational. (It crashes when I write to the socket with a message about creating a thread for a different parent, but I won't get into that here).
    If you get a warning like that then this is something to investigate. This is very likely contributing to the problem.

    Quote Originally Posted by ocgltd View Post
    In my single thread I connect a readyRead signal from the tcpsocket to a readCommand slot inside the same thread.
    So the socket and the receiver object belong to the same thread?

    Quote Originally Posted by ocgltd View Post
    From what I've read, you should not have a signal from inside a thread connect to a slot in the same thread. Is that right?
    No. If that were true it would be impossible to write single threaded applications.

    Quote Originally Posted by ocgltd View Post
    If that's the problem, how should I design this server?
    Unless it is an exercise for multithreading my suggestion would be to do it single threaded.

    Quote Originally Posted by ocgltd View Post
    Do I have to create 2 threads for each connection?
    No, do not access a socket from two different threads. One per connection is more than enough, i.e. one often doesn't need a new thread per connection.

    Cheers,
    _

Similar Threads

  1. Replies: 37
    Last Post: 14th December 2012, 10:50
  2. Thread safety with signals and slots
    By blooglet in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2012, 16:03
  3. Are signals and slots thread safe?
    By Cruz in forum Qt Programming
    Replies: 12
    Last Post: 21st April 2011, 15:57
  4. Confused about Signals and Slots
    By duave in forum Newbie
    Replies: 1
    Last Post: 4th October 2010, 07:58
  5. QThread - multi threaded signals and slots
    By rishid in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2008, 03:47

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.