Hello, welcome back.
In this lesson, we're going to see how to change the priority of a task during runtime, I'll make
a copy of our last project, number four, top priority here.
Copy and then paste over here.
And then I'll call this number five.
Change in priority in runtime, I'll call this.
OK.
So I'm going to open this and then.
I'm going to open or may not see, right, so let's see in our task creation, we have Gnarl here and
now here we made use of this Fastenal.
We know this now.
As for the the task parameter, if we have any task parameter, this where we pass it, this second
null is for a variable to hold the task.
Hondo, we've never used the task Hondo.
However, in this lesson we're going to introduce the tasks the task handle the task undo allows us
to configure the task during runtime.
We need a handle of the task.
You can think of something you hold in order to control something.
So we need a handle of the task to be able to configure it or make changes to it.
So I'm going to come over here.
And then I'm going to I'm going to have the task Hondo here.
Task Hondo, let's see, I'm going to see task, I'm going to create a task Hondo type.
And then I call this blue Hondo for blue Tosk, red Honda for Red Tosk.
And then we have green Honda.
OK, so I'm going to pass these condos to the various tasks, so over here, when we created Blue Task,
I'll copy this.
The last document is supposed to be the position of the ampersand over here.
And then there's the name of the variable essentially parsing the address here.
So Red would have this.
And then green controversy would have this.
OK, so we have the task, hundreds now.
Now we come to our task functions, we said we want to change the priority of the tasks.
We're going to start off the C, we're going to start off by let's say we give Blue a priority of two
rights during creation.
Blue has a priority of two.
Red has a priority of two as well.
And then let's say Green has.
Greenhaus, a priority of one, so we know what's going to happen, essentially based on our previous
experiment, when we run the program, we're going to have just blue and red executing and green won't
have any time at all to execute.
OK, so what are we going to do is we know blue is going to be executed.
We're going to go into the task function of blue and then we would use that to set the priority of green
higher.
So over here, when we start off, green has a priority of one.
So we would go inside.
We'll go inside one of the execution, was it a blue or red and then change the priority of green shirt
that so I'll come over here to red.
So red increment, it's variable and then let's say it delays for a while.
It will delay for let's say, I'll say four over here.
I'll put a local variable here and I say, for I equals zero,
sorry about the composition in the microphone equals zero.
I less than, let's say, a value such as seven hundred thousand.
I plus plus there's a reason I'm not using delay functions, actually delay functions here because.
When we get to where the task goes to sleep, we would use the delay, you use the delay to essentially
put the task to sleep and by putting it to sleep, the scheduler removes it from running states.
We don't want that.
We just want we want to simulate delay.
We don't want to put it to sleep.
That is why we use in for loops here.
You would understand when we get to the bit where we are, we put in the task to sleep, OK?
So over here we delay for this amount and the function for the priority is the V task priority set function.
And this function takes the argument.
The first argument is the handle of the task, whose priority you wish to set.
And the second argument is the new priority level you want to assign to that task.
So come over here, I'll say.
The task priority said.
And then I want to said this to green task, so I use the green Honda and then I want to assign priority
three to it like this.
OK, so let's run this and see what we have.
Click over here to build.
OK, that's an issue, but as I say.
The task.
We top priority set.
What does it say to function as a green Honda, Undeclared says?
OK, I'm using a cocktail here, it should be a small H.
Click here to build.
OK, it's both successfully.
Click here to get onto the board.
Yes, well, we need number five here.
OK.
Click here to switch.
OK, so let's see what's going to happen or what we expect to happen with created three tasks, the
first task as priority of to the second highest priority of two.
The third one is the green task, the highest priority of one.
So when we start, we're going to have just these two tasks running.
But inside a red task, we call a function to set the priority of green task to three.
So all of a sudden the priority of green becomes the highest.
So let's see what happens.
Click to run.
OK.
So that is what we ended up with these two Froese.
Essentially so red.
OK, imposed this.
So blue started blue and red, they started with the same priority level, so they were sharing the
time blue was expected to execute for X amount of time, say ten milliseconds.
After ten milliseconds, red would execute for ten milliseconds and then blue blue would execute again
for ten milliseconds and then ready to execute again for ten milliseconds, then to continue going back
and forth like that because they have equal priority and then the other one has a lower priority.
But this will happened blue finish executing for the assigned amount of time or known as the time cancer.
But when Red tried to consume all of it time, when Red started executing its first executing its first
execution, the first line is to increment the variable and then wait for a bit.
And then the second line is to execute this instruction, which essentially said to the priority of
green to three.
So when that happened, red was taken off the color red was taken from the time time was taken from
red.
So red was moved out of the running states.
All of a sudden Green had the highest priority.
So Green started executing and green is going to execute forever.
So because now green house priority of three, the rest just have priority of two.
So you see just green task executing.
So we've essentially changed the priority of green during runtime, right?
Very easy.
Very straightforward.
These are just the basics.
It's important to to get familiar with the basics, every aspect of it, before you start building a
full project out of them.
So, yeah.
So we've seen how a task can can change the priority of another task.
What if a task wants to change its own priority?
How about that?
So what are we going to do known as?
I'm going to comment this out.
Well, I could have created a new project for this so that you have the two projects for revision,
but it's such a tiny change, I think you would remember.
So I comment this out.
And.
What I'm going to do is.
I'm going to start off.
You know what, you know what's just bear with me, I think for your revision, I've done this thing
for four years.
It's important for students to have these projects separate.
I'm going to create a new project just to demonstrate that I'll save this as it was, and then I'm going
to come over here.
And then creates a new project.
OK, so come over here.
Copy and then paste this over here.
Number six.
And then change in own priorities.
I call this.
OK, so I'm going to open this.
Right.
So what are we going to do here is over here, I'm going to start off by keeping blue.
Blue is going to be created with priority number two.
The other one red who have priority one in green will have priority one.
So when we start just blue be executed.
But what are we going to do is we're going to let Blue drop down its priority when the program starts.
I'm going to clean this from the previous projects.
So I'm going to go to the the task function of blue and I'm going to say sets priority of blue to one.
So blue is going to increment just one.
Its profile would increment just once and then we're going to drop the priority of blue to one.
And when the priority of blue drops to want everyone to have access to time, everyone would run for
equal amount of time.
Let's see.
So to do this, we use the same priority V task.
Priority said.
The first argument is now, and the second argument is the priority level, so if you want to change
the priority of the task from which you are calling this function, you pass null.
So I can either personally hear or I can pause blue Honda, remember Blue House to I can see blue Honda,
but because I'm in blue, I can simply say no.
Okay, I'll click here to run.
And then click here to do it on our click here to build and then this one to run, this number six.
I say, OK, over here.
Switch.
So let's see.
OK, so as you can see, they're all running, but we know how we started, it started with blue.
It started with.
It started with blue incrementing once and then setting the priority for the rest.
Right.
But it looks as if.
These two red and green are running more than blue.
The see?
OK, so I suppose what happened is when the priority of blue dropped, the priority of blue became the
the same priority as red and green, but blue went to the back of the queue, so red and green had to
execute before blue had its chance to execute.
So that is why I would see this disparity here.
Right.
Because it's counting.
It's yeah, it's cumulative.
But now they are all executed for equal amount of time because they have equal priority.
Right.
So this is a with changing TASC priorities will conclude the lesson here.
If you have any questions at all, you should leave them in the questions and answers area and I'll
see you later.
Finding the course useful or if you have any feedback at all, do leave it in the in the review area.
I'll see you have a nice to.