Is there a point to multithreading?

后端 未结 11 796
无人共我
无人共我 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 17:58

    Much of the multithreading is done just to make the programming model easier when doing blocking operations while maintaining concurrency in the program - sometimes languages/libraries/apis give you little other choice, or alternatives makes the programming model too hard and error prone.

    Other than that the main benefit of multi threading is to take advantage of multiple CPUs/cores - one thread can only run at one processor/core at a time.

    0 讨论(0)
  • 2021-01-02 17:59

    Most of the answers here make the conclusion multicore => multithreading look inevitable. However, there is another way of utilizing multiple processors - multi-processing. On Linux especially, where, AFAIK, threads are implemented as just processes perhaps with some restrictions, and processes are cheap as opposed to Windows, there are good reasons to avoid multithreading. So, there are software architecture issues here that should not be neglected.

    Of course, if the concurrent lines of execution (either threads or processes) need to operate on the common data, threads have an advantage. But this is also the main reason for headache with threads. Can such program be designed such that the pieces are as much autonomous and independent as possible, so we can use processes? Again, a software architecture issue.

    I'd speculate that multi-threading today is what memory management was in the days of C:

    • it's quite hard to do it right, and quite easy to mess up.
    • thread-safety bugs, same as memory leaks, are nasty and hard to find

    Finally, you may find this article interesting (follow this first link on the page). I admit that I've read only the abstract, though.

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

    No. You can't continue to gain the new CPU cycles, because they exist on a different core and the core that your single-threaded app exists on is not going to get any faster. A multi-threaded app, on the other hand, will benefit from another core. Well-written parallel code can go up to about 95% faster- on a dual core, which is all the new CPUs in the last five years. That's double that again for a quad core. So while your single-threaded app isn't getting any more cycles than it did five years ago, my quad-threaded app has four times as many and is vastly outstripping yours in terms of response time and performance.

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

    Your question would be valid had we only had single cores. The things is though, we mostly have multicore CPU's these days. If you have a quadcore and write a single threaded program, you will have three cores which is not used by your program.

    So actually you will have at most 25% of the CPU cycles and not 100%. Since the technology today is to add more cores and less clockspeed, threading will be more and more crucial for performance.

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

    I don't know if you have payed any attention to trends in hardware lately (last 5 years) but we are heading to a multicore world.

    A general wake-up call was this "The free lunch is over" article.

    On a dual core PC, a single-threaded app will only get half the CPU cycles. And CPUs are not getting faster anymore, that part of Moores law has died.

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

    In the words of Herb Sutter The free lunch is over, i.e. the future performance path for computing will be in terms of more cores not higher clockspeeds. The thing is that adding more cores typically does not scale the performance of software that is not multithreaded, and even then it depends entirely on the correct use of multithreaded programming techniques, hence multithreading is a big deal.

    Another obvious reason is maintaining a responsive GUI, when e.g. a click of a button initiates substantial computations, or I/O operations that may take a while, as you point out yourself.

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