Click here to Skip to main content
15,891,757 members
Please Sign up or sign in to vote.
4.25/5 (4 votes)
what constitute proper use of threads in programming?

I am tired of hearing people recommend that you should use only one thread per processor, while many programs use up to 100 per process! take for example some common programs

vb.net ide uses about 25 thread when not debugging<br />
System uses about 100<br />
chrome uses about 19<br />
Avira uses more than about 50


Any time I post a thread related question, I am reminded almost every time that I should not use more that one thread per processor, and all the programs I mention above are ruining on my system with a single processor.
What constitutes proper use of threads in programming?
Please make general comment, but I'd prefer .NET framework
thanks
Posted
Comments
harish85 29-Jun-11 19:04pm    
Sorry , unintentionally while reading this blog, I voted "1" for this question, Is there way to revert back.
Pete O'Hanlon 30-Jun-11 3:36am    
Just vote 5 to change your vote.
harish85 30-Jun-11 4:06am    
Thanks , I did that .

The "proper" number of threads depends quite a lot on the situation and usage. There is no one size fits all answer. May people, especially beginners, think adding more threads improves performance, but can actually have the opposite effect.

For a windows app, only one thread can update the UI at a time so it doesn't matter how many you have running. Yet if there is some intense background processing that needs to occur, perhaps reading a file and doing some calculations, than another thread or two may be useful.

You can't use a comparison of other applications and the number of threads they use because you have no idea what they are doing.
 
Share this answer
 
If you have a task in your application that you need to perform and it's going to take a persceptible amount of time then it shouldn't be done on the UI thread, so you need to put it onto a background thread. Anybody who says you shouldn't obviously works for the Microsoft Outlook team.

Basically, any time you need to perform a task (such as listening for TCP requests) that shouldn't block the application, consider creating a thread. If you need to do something that occurs only at a set interval, then consider putting the wait for that interval on a separate thread - and trigger the process on that thread.

These are all things I've done in applications, and I've never had complaints about doing it.
 
Share this answer
 
There are many reasons for using threads besides perfomance.

For example, suppose your program does 22 different operations, each of those operations is a long sequential operation and at various points in the sequence it has to wait on input or fetch data over a network, or...

If those operations don't need to share data much, then the easiest thing to do is run those 22 different operations in 22 different threads.

It might not be the fastest performance wise, but it's the simplest implementation wise.

Sure, you could implement the same thing in fewer threads, but the code wouldn't be as easy to read or maintain.


And the datum "don't use more threads than you have cores/processors" is just outright false.

The real datum should be something like:

To get optimum performance the optimum ratio of threads to cores is dependent on the average time any thread is going to be waiting on i/o compared to the time it takes to swap threads of execution in any one processor as modified by the number of shared resources the threads will need to have execlusive locks [for how long], taking into account the OS's algorithm for allocating cores.

In other words: if your threads are going to have long waits for input and they don't have any shared resources that the need to exclusively lock (or rarely need access to), then you probably will get better performance with more threads.

But if your threads have shared resources that they need to exclusively lock often, you might actually end up with better performance if you have a single thread so you don't have to lock / wait for the resource(s).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900