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
One of the most important uses of multithreading is in GUI programming. If you only had a single thread, what happens when you click a button? You would have to wait for whatever action that button fired to complete before control returned to the GUI. To put that in context. If your browser only ran in a single thread and you wanted to download say, a Linux ISO, you're entire browser would be unusable for the duration of the download as the single thread would be taken up with the download and wouldn't be available to respond to user actions. You couldn't even cancel the download.
By using multiple threads, you can continue using your browser while the download occurs in the background.
There are plenty of other uses that can speed up a program. For example, searching a large dataset. You can divide it up into chunks and each thread can search a chunk. You can then join on those threads completing and collect the results.
Also, semaphores aren't always necessary. It depends on what you're doing. If you have multiple threads consuming tasks from a single work queue, you want to make sure a job is removed from the queue before another thread can request a job so that you're not assigning the same work to 2 threads. In that case you use semaphores to make your work queue "thread safe". On the other hand, hootsuite or one of those other social media desktop clients could (don't know if they do) run a thread for each platform you're connected to so that you can fetch updates from multiple platforms in parallel.
In some cases, multi threading slows down the application because locking and context switching requires some cpu resource, but overall application performance would greatly improve when you target multi core or multi cpu machine, because the only way to distribute your code across cores/cpus is to use threads.
In single core machines, running a single task with multiple threads will surely cause slow down due to the fact mentioned above.
Another usage of threads is to keep ui responsive, imagine a scenario when you need to perform a heavy I/O operations, such as reading from a device, fetching data from network etc. if you perform those operations in main thread, your ui will be blocked while I/O operation is running. You can avoid ui blocking by running I/O operations in different thread. Probably, that was meant with "speeding up the application".
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.