Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

multithreading in single cpu  RSS feed

 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I have a doubt that increasing the no of threads to process something...whether increases performance in a single cpu machine.
The threads are not going to wait on anything.
In this case both single thread or multithreaded will fairly give the same performance.Am i right,please clarify

Thanks
Praveen SP
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praveen:
The threads are not going to wait on anything.
In this case both single thread or multithreaded will fairly give the same performance.


If you are sure that the threads are not going to wait for anything like monitor locks, I/O etc. then increasing the number of threads may decrease the performance because of thread scheduling over heads.
OS needs to schedule the live threads in a way that every thread of equal priority gets an equal opportunity to run. So, if there are more live threads then the OS has more threads to schedule and hence slicing the time of the running threads.
 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi nitesh

Thanks for your reply.Your reply clarifies my doubt.Now is the degree of multithreading depends on the no of cpu available alone,provided that no thread will wait for anything?

Say If my machine has two CPU's what if I use three threads to process a task.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on what the processing consists of. If it's purely CPU work, then there's a chance that performance will decrease, due to the overhead of partitioning the work and thread scheduling and switching, just like Nitesh said.

If some amount of I/O (file, DB, network etc.) is involved, then performance might be better, because the CPU-bound parts of one thread could be executed simultaneously with the IO-bound parts of some other thread.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say If my machine has two CPU's what if I use three threads to process a task.

It might still be beneficial if the CPUs are multi-core CPUs, or have multi-threading capabilities. Or (as I said above) if there's any I/O.
 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya thanks ulf for your clarification
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing to keep in mind is *perceived* versus *actual* performance. For example, using a thread to show a progress bar to the user actually *decreases* performance (if just a little bit), but typically makes the process to *appear* faster.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!