Results 1 to 4 of 4

Thread: Organizing multiple threads

  1. #1
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Organizing multiple threads

    Hi, I wrote a program that acts like a "bridge" between various clients and their server. The programs acts this way: for each connection from the clients, he opens a socket on the server. After this he starts listening on both sides. When something arrives from the client, the program simply places it on the socket of the server and send it away. When something arrives from the server, the program reads it, change its structure and send it to the client. Easy.
    I have read around that is not always good to use one thread for one socket and that it's better to use a "multiplexer" style in which a thread handles multiple connections... Well, how to know whether to choose the "multiplexer" solution rather than the one-one solution?

    Thanks!

  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: Organizing multiple threads

    With Qt you can handle many connections in a single thread using signals and slots.

    Basicaly, it's not good to use a separate thread for each connection if you expect to receive many connections at a time (nobody wants to have 100 threads of the same process in their system). On the other hand, if you expect not more than few connections at a time, it's not worth using threads because you can safely handle all the connections from within one thread (for example using the select() call or Qt sockets).

    Threads are good if you want to separate connections from each other, so that they don't interfere each other. On multiprocessor systems having multiple threads is the way to go. Otherwise, either use a single thread for all connections or make a hybrid system with thread queues (like apache does, for example).

  3. #3
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Organizing multiple threads

    Quote Originally Posted by wysota
    Basicaly, it's not good to use a separate thread for each connection if you expect to receive many connections at a time (nobody wants to have 100 threads of the same process in their system). On the other hand, if you expect not more than few connections at a time, it's not worth using threads because you can safely handle all the connections from within one thread (for example using the select() call or Qt sockets).
    Ok, but if use threads, don't I obtain a speed-up of my process? I lose memory but I increase my speed ,no? So I suppose I have to choose the structure of the program depending on what is the priority for me,right?
    Moreover, are there some *quantitative* parameters that tell me how much speed I lose depending on how many task I run in a thread?I mean: how to understand when is better to start using threads (as a function of my needs).
    Hrmm , I hope the last paragraph is comprehensible...

    Quote Originally Posted by wysota
    Threads are good if you want to separate connections from each other, so that they don't interfere each other. On multiprocessor systems having multiple threads is the way to go. Otherwise, either use a single thread for all connections or make a hybrid system with thread queues (like apache does, for example).
    Threads are also necessary to achive a real time connections between different users (like MUDs for examples), right?

    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: Organizing multiple threads

    Quote Originally Posted by vratojr
    Ok, but if use threads, don't I obtain a speed-up of my process? I lose memory but I increase my speed ,no?
    Only if you have more than one CPU in your server. Otherwise you lose both speed and memory.

    So I suppose I have to choose the structure of the program depending on what is the priority for me,right?
    Yes, but the priority here is not speed really. You can achieve almost the same speed without threads if you design the data flow through the socket the right way (meaning that you cycle through connections instead of serving one connection at a time).

    Moreover, are there some *quantitative* parameters that tell me how much speed I lose depending on how many task I run in a thread?I mean: how to understand when is better to start using threads (as a function of my needs).
    There are no such parameters. At least none that can be measured by you. You can experiment and see what's better for you. The main advantage of using a thread per connection is that you can use blocking calls without blocking all other connections. With Qt in most cases non-blocking calls are used (which is less comfortable for the programmer but more efficient), so unless you force the blocking behaviour (for example by calling methods like waitForBytesWritten), you don't benefit from that advantage.

    Threads are also necessary to achive a real time connections between different users (like MUDs for examples), right?
    No. Most MUD servers are single threaded or at least handle all user communication in a single thread. If you wanted to have separate threads for that, you'd have several hundred threads from your application constantly active. This is a huge loss of efficiency (also because you have to synchronise the threads).

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

    vratojr (25th May 2006)

Similar Threads

  1. how to corss compile for windows in Linux
    By safknw in forum Qt Programming
    Replies: 24
    Last Post: 13th May 2006, 05:23
  2. Multiple shortuts for the action
    By Lemming in forum Qt Programming
    Replies: 6
    Last Post: 6th April 2006, 22:29
  3. [QT4] threads, signals, and slots, please help.
    By ucntcme in forum Qt Programming
    Replies: 12
    Last Post: 25th January 2006, 14:23
  4. Replies: 25
    Last Post: 15th January 2006, 00:53

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.