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
Removing 'parallel threading' from the concept of multithreading does make it pointless — if you don't allow the threads to execute at the same time then all you've got is one stream of processing that spends a lot of time hopping about in the OS's scheduler.
That the threads can operate in parallel is the whole performance gain. You should optimise your code so that semaphores are rarely used, if ever — you're right that they're expensive. A common approach is thread pooling and event loops; suppose you had 2,000 objects that you wanted to mutate, you'd push 2,000 associated tasks to the thread pool. The thread pool would ensure that the individual actions are performed on such threads as become available, when they become available. If it's then able to post an event into a defined event loop when the work is done, there are no explicit semaphores in your code at all.