Multiple threads and performance on a single CPU
Asked Answered
C

6

16

Is here any performance benefit to using multiple threads on a computer with a single CPU that does not having hyperthreading?

Colostomy answered 6/9, 2008 at 18:21 Comment(0)
P
33

In terms of speed of computation, No. In fact things will slow down due to the overhead of managing the threads.

In terms of responsiveness, yes. You can for example have one thread wait on an IO operation and have another run a GUI at the same time.

Provender answered 6/9, 2008 at 18:24 Comment(2)
In an academic sense you are absolutely correct. However, no program on earth is 100% CPU-work which means in practice, multiple threads can have a positive impact even though theres only one computing unitWildfire
Can this answer be more elaborate? For example what benefit has multithreading as in compared to multiprocessors?Carpo
P
11

It depends on your application. If it spends all its time using the CPU, then multithreading will just slow things down - though you may be able to use it to be more responsive to the user and thus give the impression of better performance.

However, if your code is limited by other things, for example using the file system, the network, or any other resource, then multithreading can help, since it allows your application to behave asynchronously. So while one thread is waiting for a file to load from disk, another can be querying a remote webserver and another redrawing the GUI, while another is doing various calculations.

Working with multiple threads can also simplify your business logic, since you don't have to pay so much attention to how various independent tasks need to interleave. If the operating system's scheduling logic is better than yours, then you may indeed see improved performance.

Plante answered 15/9, 2008 at 21:10 Comment(0)
A
8

You can consider using multithreading on a single CPU

  1. If you use network resources
  2. If you do high-intensive IO operations
  3. If you pull data from a database
  4. If you exploit other stuff with possible delays
  5. If you want to make your app with ultraspeed reaction

When you should not use multithreading on a single CPU

  1. High-intensive operations which do almost 100% CPU usage
  2. If you are not sure how to use threads and synchronization
  3. If your application cannot be divided into several parallel processes
Artistry answered 6/9, 2008 at 18:21 Comment(0)
A
2

Yes, there is a benefit of using multiple threads (or processes) on a single CPU - if one thread is busy waiting for something, others can continue doing useful work.

However this can be offset by the overhead of task switching. You'll have to benchmark and/or profile your application on production grade hardware to find out.

Angelinaangeline answered 16/9, 2008 at 21:17 Comment(0)
A
1

Regardless of the number of CPUs available, if you require preemptive multitasking and/or applications with asynchronous components (i.e. pretty much anything that combines a responsive GUI with a non-trivial amount of computation or continuous I/O processing), multithreading performs much better than the alternative, which is to use multiple processes for each application.

This is because threads in the same process can exchange data much more efficiently than can multiple processes, because they share the same memory context.

See this Wikipedia article on computer multitasking for a fairly concise discussion of these issues.

Abet answered 6/9, 2008 at 21:1 Comment(0)
C
1

Absolutely! If you do any kind of I/O, there is great advantage to having a multithreaded system. While one thread wait for an I/O operation (which are relatively slow), another thread can do useful work.

Chaetognath answered 17/9, 2008 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.