There are several ways to make better use of the capabilities of your computer. Multiple threads allow your application to perform multiple things at the same time. With this power comes a lot of responsibility and you should also realize that it’s not the answer to everything.
Here’s three common ways to speed up your application:
- Get a faster computer. Computers have steadily increased in speed and power but we can’t count on this continuing.
- Write better and more efficient code. Use better algorithms. The power of selecting the proper algorithm cannot be stressed enough. This is big. And for a large enough problem, will always win.
- Do more things at the same time. This is multithreading.
Multithreading is not just for speeding up your application though. This episode started getting too long to try explaining everything. Instead, this episode explains more about why you need to understand threads and what they are. Listen to the full episode or you can also read the full transcript below.
I’ve mentioned multithreading in many past episodes and it’s time we discuss this. Let’s say you own and operate a small diner. The kind that has room for about a hundred people to sit down for lunch. With the potential for that many people arriving for lunch at about the same time, the performance and speed that you can serve the guests is important to you. How can you improve things? Here are 3 things you can try:
#1 You can give your staff the best tools possible. Ovens that cook faster, grills with more even and constant heat, order processing software to make sure no mistakes are made.
#2 You can train your staff in more efficient techniques. Show them how to prepare the tables ahead of time, how to get water ready, and how to make the best use of all the high-tech features available.
#3 You can hire more staff. An extra cook and a couple extra servers will go a long way. And maybe even a dedicated receptionist to help seat people and take care of those waiting for a spot.
If you know me by now, then you’ll expect that I’m going to find some way to relate this diner back to programming. And that’s right. For many years, we benefited from ever faster computers. Processor speeds, internet speeds, and hard drive speeds have increased. Just buying a new computer has been enough to see a noticeable improvement in software. About the only thing that hasn’t kept up has been memory speeds. Don’t get me wrong, memory has improved, just not as fast as everything else. But here’s the thing, we can’t rely on computers getting faster all the time. This matches the first suggestion for your diner to give your staff the best tools possible.
Depending on the size of your problem, a better algorithm will give you better results. And a good algorithm vs. a bad algorithm can really make all the difference. In other words, just by writing your code smarter can help make it faster even on older machines. This matches the second suggestion for your diner to train your staff to work smarter and more efficiently.
And the third suggestion to hire more staff matches multithreading. While processor clock speeds are struggling to increase, you might have noticed modern computers advertised to have dual cores or quad cores. Think of each core as a completely different processor capable of acting independently from the other processors. A core is actually better than another processor because multiple cores on the same processor chip can communicate faster with each other than if they were on separate chips. You might even see high-end computers with several processors each with several cores. And on top of all this, there’s other technology that allows even a single core to be split into multiple separate processing units.
And let’s not forget modern graphics cards. These things are capable of massive amounts of processing and often just sit idle. This is a huge amount of power that you can learn how to make use of.
When you add all this up, modern computers are capable of doing hundreds or even thousands more separate tasks than used to be possible. But if you don’t write your code to make use of this, then guess what? Your application will be stuck in a single core. It’s like trying to drive your car hundreds of miles when you can only drive on the side streets. You’re missing out on the ability to get onto the highway if you don’t understand multithreading.
I’ll explain more after this message from our sponsor.
( Message from Sponsor )
I hope by now that I’ve got you interested in multithreading. But before we go any farther, let me ask you this. Would you get in your car and get onto a super fast highway just so you can get off again and visit the local grocery store that’s right next door? No, right. And what about that diner? Would you add an extra cook if your one and only cook couldn’t keep up? Seems like a good idea, right? And if it’s good to add one extra cook, what about hiring ten more cooks? See the problem?
Sometimes what’s good in one situation only makes things worse in another situation. And even when doing something does make an improvement, there’s a point where doing it again stops helping and starts making matters worse again.
Multithreading is like this. You first need to know when to use it. Then you need to know when to stop using it.
We’re not going to have enough time today to discuss everything about multithreading. This is a big topic. Consider this more of an introductory episode. I’ll just explain today some extra information about how computers work so you’ll better understand what a thread is and how to use them.
First of all, let’s go back to those cores and assume that each core means the computer can perform a separate and simultaneous task as another core. If you have a quad core processor, then that means your computer can be executing up to four instructions in parallel. The word parallel just means doing work at the same time as some other work. These multiple cores give you true parallelism. The computer really can be running multiple instructions at exactly the same time.
In addition to this though, you also have the ability to swap one running set of instructions with a different set of instructions. The processor supports the ability to stop running some instructions at any time between instructions, save everything, and switch over to another set of instructions that was previously stopped and saved. Think of the exiting instructions as if they go to sleep for a moment. Only they don’t even know what happened. If this happened to you, then it would be like eating a spoonful of your breakfast watching TV about an upcoming sports game, and then without noticing or even thinking it strange, your next bite shows the score of the game that’s already been played. It’s a smooth transition with sometimes jarring effects.
This ability to swap running code with other code allows an operating system like Windows, Linux, or a Mac to run even more threads than the available number of cores in your machine. You see, each thread, is a running set of instructions within your code. Think of it as if a sewing thread was being routed through your code. As a method calls another method, returns from that method, runs some more code, then calls yet another method, etc. It traces a path through your code running instructions that you’ve written.
The thread can take different directions based on the results of various if statements or other flow altering instructions. It’s important to realize though that a single thread cannot be doing multiple things. All it does is execute a single instruction and then go get the next instruction to run. It needs to be running on an actual core in order to step through instructions.
When we talk of multithreading in programming, we normally don’t care how many cores your computer has. Sometimes this is important. It’s like knowing how big your diner kitchen is in order to plan how many cooks you can hire. Most of the time though, you start a new thread whenever you have a good reason for one. When we talk about multithreading, we’re normally talking about how the various threads in your application interact with each other even if they’re all sharing the same core and only one thread can actually be running at any given time. Remember that to a thread, it’s always running.