Is there a point to multithreading?

后端 未结 11 799
无人共我
无人共我 2021-01-02 17:22

I don’t want to make this subjective...

If I/O and other input/output-related bottlenecks are not of concern, then do we need to write multithreaded code? Theoreti

相关标签:
11条回答
  • 2021-01-02 18:12

    Most CPUs these days are multi-core. Put simply, that means they have several processors on the same chip.

    If you only have a single thread, you can only use one of the cores - the other cores will either idle or be used for other tasks that are running. If you have multiple threads, each can run on its own core. You can divide your problem into X parts, and, assuming each part can run indepedently, you can finish the calculations in close to 1/Xth of the time it would normally take.

    By definition, the fastest algorithm running in parallel will spend at least as much CPU time as the fastest sequential algorithm - that is, parallelizing does not decrease the amount of work required - but the work is distributed across several independent units, leading to a decrease in the real-time spent solving the problem. That means the user doesn't have to wait as long for the answer, and they can move on quicker.

    10 years ago, when multi-core was unheard of, then it's true: you'd gain nothing if we disregard I/O delays, because there was only one unit to do the execution. However, the race to increase clock speeds has stopped; and we're instead looking at multi-core to increase the amount of computing power available. With companies like Intel looking at 80-core CPUs, it becomes more and more important that you look at parallelization to reduce the time solving a problem - if you only have a single thread, you can only use that one core, and the other 79 cores will be doing something else instead of helping you finish sooner.

    0 讨论(0)
  • 2021-01-02 18:14

    That's kind of like asking whether a screwdriver is necessary if I only need to drive this nail. Multithreading is another tool in your toolbox to be used in situations that can benefit from it. It isn't necessarily appropriate in every programming situation.

    0 讨论(0)
  • 2021-01-02 18:19

    The primary reason I use multithreading these days is to keep the UI responsive while the program does something time-consuming. Sure, it's not high-tech, but it keeps the users happy :-)

    0 讨论(0)
  • 2021-01-02 18:21

    Here are some answers:

    • You write "If input/output related problems are not bottlenecks...". That's a big "if". Many programs do have issues like that, remembering that networking issues are included in "IO", and in those cases multithreading is clearly worthwhile. If you are writing one of those rare apps that does no IO and no communication then multithreading might not be an issue
    • "The single threaded code will get all the CPU cycles". Not necessarily. A multi-threaded code might well get more cycles than a single threaded app. These days an app is hardly ever the only app running on a system.
    • Multithreading allows you to take advantage of multicore systems, which are becoming almost universal these days.
    • Multithreading allows you to keep a GUI responsive while some action is taking place. Even if you don't want two user-initiated actions to be taking place simultaneously you might want the GUI to be able to repaint and respond to other events while a calculation is taking place.

    So in short, yes there are applications that don't need multithreading, but they are fairly rare and becoming rarer.

    0 讨论(0)
  • 2021-01-02 18:21

    First, modern processors have multiple cores, so a single thraed will never get all the CPU cycles. On a dualcore system, a single thread will utilize only half the CPU. On a 8-core CPU, it'll use only 1/8th.

    So from a plain performance point of view, you need multiple threads to utilize the CPU.

    Beyond that, some tasks are also easier to express using multithreading.

    Some tasks are conceptually independent, and so it is more natural to code them as separate threads running in parallel, than to write a singlethreaded application which interleaves the two tasks and switches between them as necessary.

    For example, you typically want the GUI of your application to stay responsive, even if pressing a button starts some CPU-heavy work process that might go for several minutes. In that time, you still want the GUI to work. The natural way to express this is to put the two tasks in separate threads.

    0 讨论(0)
提交回复
热议问题