[From the last episode: We looked at the different ways memory can be organized in different kinds of systemsThis is a very generic term for any collection of components that, all together, can do something. Systems can be built from subsystems. Examples are your cell phone; your computer; the radio in your car; anything that seems like a "whole.".]
Let’s look at a scenario: you run a restaurant, but you’re short on funds to hire people. So you’re your own chief cook and bottle-washer. You do everything, from prep to cooking to setting up the dining room to washing up after. Yup, makes for a long day.
Solo Stew
Today you’ve got a stew on the menu, and it needs to cook for a while. So you have the following tasks to perform:
- Cut the meat
- Brown the meat
- Prep the mirepoix – onion, carrots, and celery
- Sauté the mirepoix
- Add the meat back in
- Add liquid
- Let it cook
- Prep other vegetables to be added later
- Add other vegetables
- Set the table
- Plate the stew
- Serve the stew
Of course there’s lots of other stuff I’ve left off, but this should help with the main point: you’ve got a lot of work on your hands. You have to schedule when you’re going to do what so that everything comes together when it needs to and so that nothing – or no one – has to wait.
Stew Support
Now, let’s change the scenario. Let’s say that, due to a bequest by your Aunt Mabel, you have the funds to hire some staff after all. So you use one person to run the front-of-house – setting tables and such – and one person to assist you in the kitchen. Up above, you have to do all the tasks in some sequential order – you can’t do two things at once. But with a kitchen helper, you can be dealing with the meat while they prep the mirepoix. They can prep the other vegetables while you’re doing other stuff that we didn’t even list here (but you know needs doing).
This helps to illustrate the notion of a threadA piece of a program that can execute at the same time that some other piece of that same program can execute.. We’re doing one task above, and that is making stew. But that task is made up of a number of steps that could be handled separately – preferably at the same time that other pieces of the task are being handled. Each such separable task can be considered a thread: it’s something that can go on by itself while other things are happening at the same time.
Think of it as having a supervisor that assigns jobs, much as a dispatcher would. Maybe the assistant in the kitchen was washing some dishes and finished. The dispatcher (who, in our example, is you, and you also dispatch jobs to yourself) sees that the assistant is free and now asks them to prep the mirepoix. The assistant now goes off and handles that job on their own.
There’s a Thread in my Stew!
This may sound whimsical, but it’s serious business when it comes to computing. Threads are a big deal both in softwareIn this context, "software" refers to functions in an IoT device that are implemented by running instructions through some kind of processor. It's distinct from "hardware," where functions are built into a silicon chip or some other component. and in hardwareIn this context, "hardware" refers to functions in an IoT device that are built into a silicon chip or some other dedicated component. It's distinct from "software," which refers to instructions running on a processor.. And we’ll talk about both. But, before we go into any more detail, let’s move from the kitchen into the computer. We have a program to run, and it does lots of things along the way.
Let’s say we have an internet browser. Its job is to retrieve web pages for you – possibly multiple ones, in different tabs or windows. Part of it sits around waiting for you to do something – click on a link, perhaps. What kind of threads might it be able to send out? Things like:
- Go to a specified web page
- Open a new window in private mode
- Show a streaming video
- Bookmark a page
- Open a new tab
You might look at most of those and say, “Geez, those are simple tasks – it can mostly do those all by itself as a single thread without needing any help from other threads.” But a closer look shows that that’s not necessarily the case.
Send Help!
The biggest example is the streaming video. No matter what you do with the browser – opening new tabs, browsing in other tabs – the video keeps playing. So now more than one thing is happening at the same time. Another example is when you simply go to a website. That takes some time – especially these days, since it has to do all of those slow enhanced securityRefers to whether or not IoT devices or data are protected from unauthorized viewers. handshakes. While you’re waiting for the page to load, you can do other things like opening new tabs.
So you can think of all of those jobs above as being candidates for independent threads.
This is the overall idea of threads. Next we’ll look a bit closer, since not all threads are independent. And there are some specific rules as to what’s a thread and what’s not.
Leave a Reply