Single thread to multi-threaded application

后端 未结 6 1062
南笙
南笙 2021-01-03 17:05

When we should use threads in our application. In other words, when should I convert a single threaded application to multi-threaded application. Being a developer, I think

6条回答
  •  天命终不由人
    2021-01-03 17:55

    A very good to reason to "convert" is that multi-cores machines are getting the norm and it's quite sad to see old programs performing badly because they're only working on one core.

    I had a number crunching application where one part was really slow on a Mac with 16 virtual cores for a needed (and often called) sorting algorithm was only running on one core. I implemented my own multi-threaded sorting algorithm to adapt to the number of cores and bingo, instand amazing speedup.

    Such a very unoptimized behavior can clearly seen using a CPU monitor.

    So one answer to your question "when should we use thread?" is simply "when you're artificially slowing down your software by making it only run on one core".

    As a side note it's very interesting because, in this example, the complexity of the sorting algo stays O(n log n ) but the "parallelization effect" gives an amazing speed boost.

    Other reasons may be that in quite some cases correctly multi-threading your application, for example by using a well thought procuder/consumer scheme, can also help reduce some kind of resources contention.

提交回复
热议问题