How can multithreading speed up an application (when threads can't run concurrently)?

前端 未结 9 1923
忘掉有多难
忘掉有多难 2020-12-31 09:11

I\'m learning about multithreading, but after reading some tutorials I\'m sort of confused. I don\'t understand how multithreading can speed up an application.

By in

9条回答
  •  被撕碎了的回忆
    2020-12-31 10:06

    Think of threads as "things happening at the same time".

    Once you think of them that way, then it doesn't matter if multiple threads are running on a single or multi-core machine. The idea is that you have more than one code path that executes simultaneously.

    Now, if we look at a single core machine, then there can only ever be one thread executing at a time as you point out. However, if you think of each thread as a context, there can be several happening: handing input, updating the display, handling network communication, doing background tasks, etc. So yes, on a single core machine, multi-threading will be slower. However, that's not the point. The point is that your application is able to handle multiple activities simulteneously.

    Finally, when you do move from a single core machine to one with multiple cores, if you've threaded yur application properly, those contexts have a much better chance of truly running simultaneously.

提交回复
热议问题