Hello, welcome back.
In this lesson, we're going to see how to synchronize multiple tasks using event groups have gone ahead
to create a new project or make a copy of a previous project and then clean it up.
I gave this the name Sinkin Tasks, which events groups number 30 over here.
OK, so we can just start we start off by defining the bits for our tasks.
We're going to have three of them like we did earlier, task one, task two and task three.
Each of them have bits, OK?
And what we're going to do is we go into.
We're going to declare the 104 event group.
OK, and then once that is done, we are going to declare the event bids for all the tasks combined.
OK, if we combine all the tasks, all the task bit, if we combine task one, which task to bit, task
three bit, we storage in a variable called all sink bits.
These are all the bits.
OK, once that is done, we come to our main function and we create a what event group.
So come over here and our great defense group, once I've done that, I'm going to create the tasks
I've created three of them, task one, two and three.
They all have the same priority and.
The same stuck sighs Once this is done, I'm going to start the shetler by calling the X starts chuch
the next task, search that function.
OK, so once this is done, we go ahead and.
We create the task functions, so I'm going to start off with task one, that's a standard task function
over here and over here.
We're going to have a local variable here to store the the return because we're going to read the events
that will be returned and then in our infinite loop.
We going to see one over here.
And then we use the function events group Sync.
Right.
So if any group sanchia.
So this function, as we can see, takes four arguments, the first argument is the handle of the event
group.
The second argument is the bits to set.
So the idea is that each task would set its bit and then wait for the rest of the bit.
So task one would set the task one bit.
And then the third argument is the bits to wait for.
So task one will wait for all the bits, in our view, X or sing bits to be set.
This one that has all of our bits.
So task one will set it to bits and wait for all the bits.
And over here we are giving Portsmouth's delay.
OK.
Right, so once that is done, we would proceed, we wait for all the bids and we'll be stuck here and
then until all the bids are set, then we'll proceed.
So then we can use on if condition to check whether all the bits are set.
So we come over here and we see if what is returned, which is our you x return and all bids Singh bids
equals all being bids, meaning all of them said, then we can do something here.
So basically, all of the tasks are synched, right?
So this is going to be the same code for all of the other task, just that they will set their own bit
and check.
OK.
So what I'm going to do is I'm going to make a copy of this.
Going to make a copy of this.
And call this task to an over here task to here will set its bit task, too, and wait for the rest.
The same for TASC three, TASC three was set, it's a bit ago, this task three.
First, it would set its bit and wait for the rest, but over here we can take the opportunity to print
something.
And the reason I don't want all of them to print something is because our UAT is a single resource.
It requires exclusive access.
If all the three task, they are synchronized, meaning they all execute what is in the if block at
almost the same time.
So if all of them are trying to print that, you are at almost the same time we know what would happen.
We saw that in our semaphores experiment.
So over here.
I can print all task.
Set.
OK, and we'll find a controlled experiment to show what happens if this is not the case.
OK.
And what we have to do now is take the function prototypes to the top of the file, a copy task, one
over here, and I'll drop this over here and then task to over here.
And we have task number three as well.
OK.
And, um.
Right.
So let's see what we have we can click to build.
I'll click over here to build.
It's built successfully.
And I'm going to right click over here.
And go to the blog, US, ASTM 30 to.
A select number 30.
And then they'll say, OK, over here.
And then I'll click to switch.
This old project, I'm going to clean what we have over here.
Claire.
And then I'm going to open up several ports of barge trigger.
This and I'll click to.
All task set.
Right.
OK.
So we were able to get all tasks set.
Switch.
I press this instead terminates and relaunch.
That is why we've seen this works, but I want us to.
Run this again, Cledus, and then Pressplay.
It's rather strange.
Let's stop and see.
Events, bits, so let's stop this.
And then, um.
My computer is Lodin.
Why do we have this?
OK, real time is frozen.
I'm going to add a bit of delay when we print to slow down and stabilize the print in a bit.
So over here.
I'm going to say delay for this amount and I'm going to add new line carriage return as well.
OK, I'm going to click here to build.
I see it build successfully as go to debugger.
A click to switch.
OK, I'll come over here, new line mode selector about right over here.
And then select our ports over here.
And then.
I'll click to Open and I'll click to run over here, is going to see all tasks set.
OK, so we are receiving all this.
Looks good.
So we've essentially synchronized the tasks.
One way you can check this also is to have if you could if you can enable three LEDs, if you have the
discovery board, you can enable three LEDs and place each of them in the tasks.
And you could see the LED lights on when the when we reach here, the do something could simply be turning
on on.
And you should see all LEDs come on at once at the same time.
OK.
OK, so what is the controlled experiment, how do we know it is really sinking here and not just printing?
OK, remember we said each task here, this function inherently makes the second argument.
GetSet What I mean by this is when we call X Events group sync and we pass Task one Bethia, this sets
the bits for task one bit.
This sets this bit.
So how we come into one of them and see if indeed we still have our message print.
Right, because the idea is when we comment one of them task, one bit won't be set.
So when we get here where the message is printed, it's waiting for all the bits.
So we expect that task.
One bit is not set.
So waiting for all the bits should wait forever.
So what I'm going to do is I'm going to highlight this and control to comment this out and I'm going
to click here to build.
It's built successfully, the one in is because we're not using this variable, but this is a controlled
experiment, so we know that we click here.
To go to debugger, click to switch.
OK.
So let's run.
I'm going to press play over here, and as you can see, we have this system initializing.
We've printed just this.
And what is happening is that over here, they are all waiting.
Task to task three, they are waiting for all the bits of bits to be synchronized, remember, all bits
sink, they are waiting for all bits to be set and we know one of them is commented out, so we would
never guess it.
So we'll never have this printed.
So this is what proves that indeed synchronisation is what is going on.
OK, so we can comment what we've commented out.
Over here.
Right.
So this is it, this is it for synchronizing tasks, using event groups.
I'll see you the next lesson.
Have a nice day.