92: Multithreading. Why Should You Care?

Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categories:

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. Transcript 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 hug