Skip to main content
Loading Explore guides…
Threads · Acedra
Home
Explore
Computer Science
Threads
Sign in to save
Chat
If YouTube asks you to sign in, open the video on YouTube.
Watch on YouTube
Reload player
Transcript
246 segments
Copy
Hide video
0:26
OK, I think that's the recording and the transcription starting.
0:33
Yeah. OK. So that's recalled, that's recording as usual. I will up those upload these onto my YouTube channel. I didn't realize I was late with the 1-2 weeks ago, but everything should be up to date now.
0:49
So if you want to go back and look at any of these things. Just go to YouTube channel and if you're not sure where the YouTube channel is, again there is a link.
1:02
On teams.
1:06
Which won't show up here on Firefox that doesn't like meeting bed although. And websites in there. But trust me, that's where it is.
1:20
OK, so as you no doubt saw today, we are following up about threads now. Hey, to the relief of all, this will be a shorter lecture than normal.
1:32
Part of the reason for that is.
1:35
A lot of this stuff about threads.
1:38
Is highly technical in terms of how we implement them. In other words, there's lots of programming stuff in there and I've said this before. If you don't know programming, you're not sure, but programming, don't worry about it. You won't be required to know that.
1:55
What it does mean, however, is that I've taken out a lot more of the stuff than I normally do, so this is quite a short lecture today.
2:06
And I'm only going to cover. The basics.
2:11
And you 4 threads, we don't need to go into great detail.
2:17
Guess what we're talking about, really. Is.
2:23
The idea that we started talking about last week. That process. For some processes it might be helpful to do more than one thing at once.
2:35
But it might be helpful for a game to run a thread that looks to see whether you're using the keyboard so that you can shoot and move.
2:47
I don't know 30 to be updating the screen so the screen can be updated.
2:53
You don't want to do those one after the other that we were talking about before. Because clearly there will be a knock on in terms of the speed, are we looking at the keyboard? Are we looking at the screen and jumping one to the other? So if we have a multi core system or a multiprocessor system?
3:13
We can. Take the parts all that process.
3:19
Where the helpful to run them in parallel and do so now. Sometimes they are different. Threats. The keyboard thread the display third, sometimes they are exactly the same thread.
3:33
So if you're doing a render on a video, you might want to.
3:40
Absolute the data. You're running the same algorithm, but you Chuck the same data to multiple threads so that it can execute in parallel and come back more quickly.
3:53
So there's lots of places where we might want to do that. Games, images, network, traffic, IO, all of these things would benefit from having threads involved.
4:06
But as the saying, there's no real.
4:10
Trick too. It's simply saying, can we take some of the code?
4:16
Can we tease that out? And make it into something that we can run at the same time.
4:22
Which is why that frost. Slide there starts talking about thread libraries.
4:33
Now the ones for this to happen. The operating system has to provide the services to have threads.
4:40
And the programs that are written have to use those services. So it's an extension onto the API that we've been talking about before, which is why I was saying.
4:52
It's about programming as well, so don't worry if you're not sure but programming.
4:58
Just trust me that if you have programming this thing, there are services available to do it and that you can work with them.
5:14
To most systems these days are multithreaded.
5:18
Why? Because most modern computers these days have the ability to run multi threaded processes. They either have multiple calls or multiple processors, so because the hardware has advanced, the operating system has advanced to support the hardware.
5:37
And we see that will tell you we see.
5:40
Very simple level I and you print A comes out it's colour. So we put in a driver for the operating system to support a colour printer. It's very basic thing about the operating system evolving to take note of new hardware. I've got a track ball rather than a mouse.
5:59
And it's wireless. So I had to install a driver both for the Bluetooth connection and because it's a a trackball rather than a mouth.
6:11
And it's got a scroll wheel which basic mice don't have. I know most often do now, but that used to have so that's an extra part of the operating system that had to be added. So it's not unusual for operating systems to add an extra stuff. To support.
6:29
Hardware and that's all this is.
6:33
It's the operating system saying, well, maybe we want to update the display more quickly or go and get some data.
6:41
Or do some background spell checking. You ever wondered why? What can immediately put the blue WAVY lines on the red WAVY lines as you're doing it? It's because it runs a spell check thread or a grammar thread in the background.
6:56
And can then say ohh you've got a problem here. Which then? Feeds into the. Display thread to update your Word document.
7:07
Why don't we run multiple processes for this? The idea is if we run a thread it's. Least resource heavy.
7:17
In other words, we can use a lot of the same things, and you'll see that later.
7:22
So we're not having to lose in a whole new.
7:26
Process with all the overhead that we saw last week.
7:30
We can simply identify some extra code, share the code base, share the memory, share the data, and that makes a lot quicker to run.
7:44
The most threads run.
7:48
As a kind of.
7:50
Client server approach, so the kernel supports multiple threads. The client requests and new thread at the server the third created and then go back to.
8:04
And listening for new ones. So the client here is a process servers the kernel. Not always, but just for the sake of this discussion.
8:14
And the canal? Then create a new thread.
8:18
Just see that really quick.
8:22
Sometimes you can get processes that. Stop. Either because they were meant too like this PowerPoint presentation that stopped because. And I haven't pressed the button.
8:35
We can also get processes that stop because of other reasons and that can stop anything else happening.
8:45
So if you have multithreaded process, you can continue to have their response just because something's happening in the background and Excel spreadsheets been calculated doesn't mean you shouldn't be able to update.
9:01
The system so that you can see that the IT hasn't crashed.
9:07
The thing that turns can do is share the resources. So like I said about the word spell check.
9:13
The data itself is shared, shared for the editor. It's shared for the display it's shared for the spell check. It's all the same data. We don't have to. OK, load all that and as a new process and have all that overhead, we just share that same stuff.
9:31
And as we also saw last week, if we had another process and wanted to share that data, we would have to pass it as a message. That would also have overhead, so this way we can share all the stuff that we already have in there, so it's much cheaper in terms of process time in terms of CPU time and terms of, well, any metric that you want to use. For your computer, it's much cheaper to go to a new third, then go to a new process.
10:04
Last thing, of course, is if you're using threads as you're hardware increases. You're an.
10:12
Process can become more efficient because it can take advantage of all these new things you've added. All these new multi cores are all these new multiprocessors.
10:25
No, it's not easy and this is better with saying earlier, but you're going. And. They're all challenges when your program using threads.
10:36
They're on there. They're in big, bold letters, if you understand what they are, great. That's the challenges. If you don't understand what they are, don't worry about it. Just remember about half challenges.
10:50
For programmers who are having to implement these threats.
10:57
So there's a couple of ways that we can do this.
11:05
We can either run things in parallel. For the system runs more than one thing at once.
11:14
Or we can have concurrency. And that's what we're talking about. Last week for things like that, a timeshare system, we have multiple tasks, but each of them has to go at its own speed.
11:30
So lots of tasks making progress, but only one thing can do.
11:35
Can make that progress.
11:38
With power, little tasks have ever we can do lots more, and there's two ways that we can do this. We can either split up the data so I said earlier about rendering and image. We can split up that image and send chunks to multiple tasks. To do that rendering.
12:02
We thought to come back and then merge it all together again, so that's data parallelism and I don't need to remind you about. The text in blue. These are things you probably want to remember.
12:15
So the data parallelism parallelism where we split the data, send out, get back all those task parallelism. We are. We actually do different tasks. The editing the spell, checking the displaying. Whatever it might be, we do all those tasks at the same time.
12:38
All of this is supported by the CPU, so we need hardware and place for that, and that hardware gets more.
12:47
Sophisticated as we move on.
12:49
That's talking about that in Oracle Spark T4.
12:53
I meant to look up when that was first brought out. But it was all you will go. And. Processors, not surprisingly, are getting more and more sophisticated, with more and more calls and more threads available.
13:09
In other words, how do is getting cheaper so our machines are getting faster by making use of this?
13:17
So there's just a simple diagram for the difference 2011. Thanks, Ryan. Over 10 years old.
13:27
To the the point stands. These are examples of processes that allow that, and as you can imagine, things are getting faster every year, so concurrent execution, that's what we're talking about last week.
13:40
Do you want to take some time then to 29324? Then you're back to task one, task 2.
13:47
With multicore systems you can split that up. T1 and T2 can go together. T3 and T4 can go together. If you have more cores, you can split them up more if you have.
14:03
Tasks that are more processor dependent, more CPU dependent. You can decide actually that you want to devote. More time to them. So we here we've just split up T1T2T3T4.
14:24
We could, though.
14:30
To say that some tasks are more important than others and assign them to a specific core so.
14:37
Rather than just splitting them up. 1234. And although we could say no, we're actually going to dedicate Core 2 to task one because that's the one we want to keep updating maybe a a fast moving display.
14:51
And have all the other tasks on the other core.
14:56
We have the choice is the point we're not. And.
15:02
It's up to the programmer to decide how they want this to happen.
15:08
Now, as I said, it is more tricky to do.
15:13
And.
15:16
So here's a a single person, and this is what we talked about in previous weeks. We've got the code, we've got the detail of any files as it's accessing within the CPU.
15:29
And again, remember the same kind of idea as the little man computer where we have program counter instruction, register address, register accumulator. Clearly more sophisticated CPU will have more than just that, but it's exactly the same concept.
15:48
Those are the registers. All those CPU's will more registers will have a stack where we can store data as well.
15:57
Where we have multi 30 processes.
16:00
We can make use of the same code, data and files.
16:05
But we can add in the hardware support to have. Multiple registers and multiple stacks so you remember last week we're talking about swapping over registers when the swap processes to get to where we were in the process. If we can keep multiple threads running with multiple sets of registers, multiple stacks, it means that all of these things can run together.
16:31
Of course that takes hardware support and hardware support costs money. Which is why some CPU cost more than us.
16:42
Are we OK with that sofa?
16:46
Yes. So our Fred is just a process basically.
16:50
A trade is very specifically not processed because a process has code, data and files.
16:59
But a thread is a part.
17:03
All the process where it can run multiple.
17:08
Multiple pieces of code if you like.
17:11
At the same time.
17:14
But it depends on which ones you've chosen to split up.
17:18
So as I see, when you're running world, you may have chosen to run the spell checker as one thread, so you can check as you're going along.
17:27
So it's not a different process, it's a part of the original process.
17:33
OK. That's it's. I'm very clear distinction there.
17:37
There's a faster threads are more likely because they're not neutral sexes. You don't have that overhead.
17:46
OK.
17:49
Yep. Yeah. Thanks. So and these diagrams, these are both processes. These boxes are both processes.
17:57
It's just that this process can do more than one thing at once, and the more than one thing at once is the thread.
18:05
Single processes that we've been thinking about before, still after it's it was only ever one and can only be one.
18:16
Alright so.
18:20
Given that we have that, presumably.
18:24
We can start thinking about how we can go faster.
18:28
And in theory you would think that. If we can afford the hardware, if we can just Chuck.
18:35
More hard we are a problem and aces. We can make a system go as fast as we want we can make.
18:43
The execution goes fast as we want.
18:46
And yes and no.
18:52
Because there are some things that we can split up and we can run in parallel, but there are some things that have to run sequentially.
19:00
So there is actually. An algorithm for.
19:06
And again, as with the programming, if you're not keen on maths.
19:11
And equations.
19:14
I'm not gonna ask you to to memorize this.
19:18
You can just take my word for it and the important bit is the S.
19:25
Ace is a cereal portion. Things that you have to do 1 after the other.
19:31
Nothing can happen till they're done.
19:34
You know you can't start rendering a failed, you load it.
19:38
Loading the file was a serial process, it just has to come in. There's nothing you can do.
19:45
She can't speak that bit up. Once it's in. You can then render it.
19:49
And then you can save it and it goes back to being a serial process. So you can't just always speed up as much as you would want the cereal portion.
19:59
There's a huge drag on it.
20:03
So we can actually work out depending on applications.
20:08
And mix of parallel and cereal parts.
20:13
How much faster we can go. So there's an example there. If the application can be 75% serial and 75% parallel, 25% cereal. Doubling the calls from one to two doesn't double the speeds. It.
20:29
1.6 times faster.
20:37
So we can get faster by adding more calls, but it's not just as simple as extra core as an extra percentage of the core.
20:47
But we do know what else.
20:51
OK, so how do operating systems cope with this? What do they do?
20:58
And again, this is part of the thing where.
21:02
You're probably going to feel that I'm. I'm.
21:05
Jumping over bits, I'm not going into great detail and I'm not. Because it's a combination of the stuff that I want to do so you understand it to a certain level.
21:17
And the bits where you would have to understand it to go away in program it and make it work so. I will do it to the first level, not the second level, but if you want to know more, of course it's all in the book.
21:32
So I said this is a bit programming and I said this is about programmers understanding how threads work and using the appropriate libraries to make that happen. And there are multiple libraries to do that.
21:48
Posix so the the Unix based ones like Lennox have 1/3 library, as does windows as does. And Java. The old a culture in slightly different ways.
22:03
And those are the queues of threads. So that's something where the programmer I said I want this to be a thread.
22:11
However, the kernel then has to support those. The operating system has to support it as part of the kernel.
22:20
So you have to map. The trends that you're created onto the kernel threads. Member will be a few glitches in that you might want to create more threads than the operating system or the hardware can support.
22:36
You might have multiple processes running.
22:40
Which can create multiple threads.
22:43
And me and me not keep running, so it gets to be very complicated.
22:49
So we've got lots of different models.
22:55
Depending on what we have so here we've got lots of views of things. So the programmer said yeah, give me lots of these.
23:02
These then go to a single kernel thread. Now there's a problem with that straight away. If any one of these things goes wrong.
23:13
It blocks all of.
23:16
And we don't really have parallelism if we've only got 1 kernel threads, I'm not running at the same time.
23:24
So.
23:26
It's it's not well used as you can imagine far more frequently used is each user thread, each one that the program has said. Yep, let's make that thread. That maps to a single kernel thread that the operating system.
23:42
Hey. Makes available.
23:48
Each of those can then run in parallel and you get that speed up that we're looking for. But eyes with any resource on the operating system, it might be restricted at some point. So in the same way as. And in the process section, it looked at last week we when we're doing timeshare, we might only give a certain amount of process or time to each process. We might do the same with threads and say no well hello process number one, you've really got 4 thirds and not getting anymore because process
24:18
2 needs them. And so this process things and so on. So there might be restrictions.
24:27
However, this is a common model and you can see there. Windows and Linux both. Use this model.
24:37
So here those are a one to one you know which uses third is going to which kernel thread. Some systems do a many to many. So there are still lots of user threads, still lots of kernel threads. But the are managed by the operating system.
24:56
To create the amount of kernel threads that you need. So again this becomes more complicated. Again, it takes more.
25:06
Resources and again, it takes more skill than your programming, which is why Windows has a whole other.
25:13
A library to support us, something called the third fibre package.
25:19
So we can give you.
25:21
I. More. But it's more complicated to write.
25:27
And this is about where I was seeing right to the start. I'm going to jump over a bit because if you're not into programming, he was sitting there going what? So at the moment, just trust me that those.
25:41
Many to one. One to one, many to many and A2 level model. And a two level model just.
25:53
Takes the most managed to many model. Through, as in the one to one model and says OK, well you can have many to many and let the operating system.
26:03
Look after it. But if you've got threads you really want to be looked after, you can design. You can assign them to be a. A single kernel thread and the technical term is binding it to the thread.
26:19
So again, it gives you all of these options and they're not easy to use.
26:24
If you're programming with threads gets tricky.
26:32
And.
26:36
The libraries that are set up to do this. I'm always easy to follow.
26:43
Apart from anything else, it has to understand the two different sets of threats the user threads and the kernel threads and how they work together.
26:52
So there's definite libraries supported. OK, set up to support these things.
27:00
One is P threads.
27:03
On the. Quite common for Lennox.
27:07
And if you've forgotten what I said a couple of weeks ago, Mac OS is a version of Unix as it's Linux, so the Mac.
27:17
K supports that as well.
27:21
The point is, it's a standard.
27:27
So this is how it says there should work, not how they. Actually work.
27:34
What's the difference? Well, the standard for a car is that it has a steering wheel.
27:42
The implementation is does the steering wheel go in the left or on the right?
27:48
So they can be two different things.
27:53
So let's give you some quick examples of how Windows and Linux cope with that.
28:00
There is an API and Windows. And as I said earlier, it's one to one mapping, so. One user thread to 1 kernel thread, so one that the programmer sets up mapping to one that the operating system looks after.
28:16
I'm gonna needs to be. As well as with processors, some identification. So like with the process of the threat as an ID and it has a set of registers and it has a stack and it has all that kind of stuff that it needs to actually run.
28:32
The fancy name for that.
28:35
Blue takes alert. Is the thread context.
28:42
So what is the state of the 3rd right now?
28:52
That's been set up so that we have a control system that starts with an executive 3.
29:09
And.
29:11
Then has a thread.
29:14
That actually runs from that.
29:17
I know the ones something like this. Yeah. I was the executive third here.
29:23
So which state is it? Where does it start?
29:27
In terms of the code.
29:30
Which process?
29:33
Starting this thread.
29:35
And all the any other information at the operating system deems necessary that executive thread points to the kernel thread. And that has to keep all the scheduling information, all the synchronization information, the data and the stack, all that kind of stuff.
29:55
And that then points to a specific thread. And you see there was split this up. So this is how the operating system looks after it. So this is in the kernel space that then points to the user space, the actual code that the user wants to run.
30:17
Linux does something similar, but it doesn't call them the threads. It calls them tasks. And it uses a clone call.
30:29
And that's a bit that allows it to share all this stuff that we saw way back at the start.
30:38
Clone says keep using the same code, the same data, the same files. But give me a new century, just as a new stack.
31:00
And Linux gives you the option of which of those you want to share.
31:06
You want to fail system. Do you want the memory space? Do you want the signal handles? You've got a message and from the keyboard or from the network, however.
31:16
Do you want your fails shared? So it gives you that and.
31:22
More detailed control that you can use.
31:30
And that you'll be pleased to know, is that.
31:35
Told you it was going to be a quick one today. I told you we weren't going to go into detail.
31:41
In part because there is no.
31:45
There's no easy. I'm grandpop with thirds. As far as I'm concerned, so I've given you the idea of them to actually use them. You sort of get a wee bit of knowledge and then you have to get an awful lot of knowledge and there's no kind of nice way.
32:01
Going up.
32:03
Knowledge.
32:06
So what I've done is give you the basics of it that we're gonna need for our assessment, but not the.
32:15
Love, knowledge that you're gonna need if you're going to program.
32:20
So any questions about that?
32:25
I know it was mentioned in the previous weeks, but could you remind me about a system call is please.
32:31
OK, system call is where the.
32:35
Excuse me where the user. Process and the operating system processes interact. So.
32:45
I am running.
32:48
Hey.
32:50
I'm gonna point.
32:51
I have a mouse as you know.
33:00
If I want to click here. Have a point doesn't do the clicking the operating system does the clicking.
33:09
So our system call is anything that the operating system looks after.
33:15
Changing the display, getting information from the mouse.
33:21
With the click.
33:23
Dating fails. It's anything that you want to provide as a service. So you don't want have a point to have to.
33:33
Rewrite. Getting files from a hard disk or or a USB or network drive. You want the system.
33:43
To have that. And then you run a festival cold to see get the file. The file is called Fred dot text and it's on network share, Barney.
33:54
Good and. Don't bother me with the details. Just run that system call to get that.
34:02
The operating system then does that.
34:06
Goes to get the fail. Takes care of all the nitty gritty of it and sends it back.
34:11
So the system calls are all the things that the operating system looks after. For the system.
34:19
This please I will file systems all that kind of stuff.
34:25
OK.
34:27
Yeah, it's great. Thanks.
34:29
Any other questions just now?
34:38
Yeah.
34:42
All good. This what I'm seeing in the.
34:46
Comments.
34:55
OK. If there are no more questions, let me stop recording.
Here to help you learn
Sign in to keep a private conversation with your course tutor.
Ask me anything about the material…
Sign in to chat