Results 1 to 5 of 5

Thread: Threads communication

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Threads communication

    My program is a server and it has two clients connected to it. Each client connected to the server is a separate thread.

    I need somehow to be able to send a message from clientA to clientB and vise-versa, remember clientA and clientB are different threads.

    How could I do this ?

  2. #2
    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: Threads communication

    Any combination of shared memory, shared variable, message queue, custom event (including signal/slot call) will be fine. Just remember to do some synchronisation of threads when appropriate.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Threads communication

    You can emit signals between threads (a queued connection).

    You could emit a signal from a client thread:
    Qt Code:
    1. signals:
    2. void messageArrived(const QString& message);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. emit messageArrived("blaa");
    To copy to clipboard, switch view to plain text mode 

    Then you have at least 2 ways to deliver the message for other clients:
    1) Create a connection between the server object (which creates and launches client threads) and all the clients. Pass the arrived message to all interested clients.
    2) Create connections between all client threads, so when a client thread emits an arrived message, all other clients receive it directly..

  4. #4
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads communication

    I am interested in doing this:

    A client connects to the server and the server creates a thread that will handle the client.

    Assume that the server has 3 clients connected, therefore 3 threads ( 1 for each client ".

    Client A wants to send message to client C, but client B shouldn't know anything about it.

    How ??? Post some code if possible.

    I'm sorry, but I am totally confused.

  5. #5
    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: Threads communication

    Did you try something by yourself? The easiest way would be to send a signal to the "main" thread which would then send a signal/custom event/call a method of one of the threads.

    If we give you a ready code, you won't learn anything and will be as confused as you were before.

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Why do some QWidgets create own threads?
    By donglebob in forum Qt Programming
    Replies: 5
    Last Post: 6th July 2010, 17:01
  3. No context switch between Threads and parent ?
    By donglebob in forum Qt Programming
    Replies: 15
    Last Post: 29th October 2008, 22:49
  4. [QT4] threads, signals, and slots, please help.
    By ucntcme in forum Qt Programming
    Replies: 12
    Last Post: 25th January 2006, 14:23

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.