Is there a point to multithreading?

后端 未结 11 842
无人共我
无人共我 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: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.

提交回复
热议问题