PDA

View Full Version : Organizing multiple threads



vratojr
23rd May 2006, 17:41
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!

wysota
23rd May 2006, 18:13
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).

vratojr
25th May 2006, 10:44
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...;)



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!

wysota
25th May 2006, 10:53
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).