Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a point to multithreading?

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? Theoretically the single threaded code will fare better since it will get all the CPU cycles. Right?

Would JavaScript or ActionScript have fared any better, had they been multithreaded?

I am just trying to understand the real need for multithreading.

like image 418
Quintin Par Avatar asked Aug 05 '10 14:08

Quintin Par


4 Answers

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.

like image 67
Henk Holterman Avatar answered Nov 03 '22 04:11

Henk Holterman


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.

like image 35
S.C. Madsen Avatar answered Nov 03 '22 06:11

S.C. Madsen


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 :-)

like image 7
Bob Moore Avatar answered Nov 03 '22 05:11

Bob Moore


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.

like image 4
Michael Madsen Avatar answered Nov 03 '22 04:11

Michael Madsen