1
00:00:00,610 --> 00:00:01,760
Hello, welcome back.

2
00:00:01,780 --> 00:00:09,670
In this lesson, we're going to see how to synchronize multiple tasks using event groups have gone ahead

3
00:00:09,670 --> 00:00:15,080
to create a new project or make a copy of a previous project and then clean it up.

4
00:00:15,640 --> 00:00:20,700
I gave this the name Sinkin Tasks, which events groups number 30 over here.

5
00:00:21,220 --> 00:00:25,840
OK, so we can just start we start off by defining the bits for our tasks.

6
00:00:26,110 --> 00:00:32,320
We're going to have three of them like we did earlier, task one, task two and task three.

7
00:00:32,470 --> 00:00:34,570
Each of them have bits, OK?

8
00:00:35,050 --> 00:00:37,690
And what we're going to do is we go into.

9
00:00:39,680 --> 00:00:42,440
We're going to declare the 104 event group.

10
00:00:46,430 --> 00:00:54,530
OK, and then once that is done, we are going to declare the event bids for all the tasks combined.

11
00:00:55,100 --> 00:01:02,450
OK, if we combine all the tasks, all the task bit, if we combine task one, which task to bit, task

12
00:01:02,450 --> 00:01:05,420
three bit, we storage in a variable called all sink bits.

13
00:01:05,730 --> 00:01:06,830
These are all the bits.

14
00:01:07,370 --> 00:01:14,810
OK, once that is done, we come to our main function and we create a what event group.

15
00:01:16,160 --> 00:01:22,100
So come over here and our great defense group, once I've done that, I'm going to create the tasks

16
00:01:22,310 --> 00:01:25,120
I've created three of them, task one, two and three.

17
00:01:26,210 --> 00:01:28,790
They all have the same priority and.

18
00:01:29,720 --> 00:01:38,730
The same stuck sighs Once this is done, I'm going to start the shetler by calling the X starts chuch

19
00:01:38,750 --> 00:01:41,660
the next task, search that function.

20
00:01:42,380 --> 00:01:46,490
OK, so once this is done, we go ahead and.

21
00:01:48,010 --> 00:01:55,360
We create the task functions, so I'm going to start off with task one, that's a standard task function

22
00:01:55,360 --> 00:01:57,310
over here and over here.

23
00:01:57,310 --> 00:02:05,110
We're going to have a local variable here to store the the return because we're going to read the events

24
00:02:05,410 --> 00:02:08,680
that will be returned and then in our infinite loop.

25
00:02:10,070 --> 00:02:13,880
We going to see one over here.

26
00:02:17,580 --> 00:02:20,700
And then we use the function events group Sync.

27
00:02:22,110 --> 00:02:22,560
Right.

28
00:02:22,590 --> 00:02:24,510
So if any group sanchia.

29
00:02:28,640 --> 00:02:34,790
So this function, as we can see, takes four arguments, the first argument is the handle of the event

30
00:02:34,790 --> 00:02:35,220
group.

31
00:02:35,720 --> 00:02:38,190
The second argument is the bits to set.

32
00:02:38,720 --> 00:02:44,390
So the idea is that each task would set its bit and then wait for the rest of the bit.

33
00:02:44,780 --> 00:02:47,350
So task one would set the task one bit.

34
00:02:47,690 --> 00:02:51,920
And then the third argument is the bits to wait for.

35
00:02:51,920 --> 00:02:57,530
So task one will wait for all the bits, in our view, X or sing bits to be set.

36
00:02:58,010 --> 00:02:59,510
This one that has all of our bits.

37
00:02:59,870 --> 00:03:03,350
So task one will set it to bits and wait for all the bits.

38
00:03:03,530 --> 00:03:05,470
And over here we are giving Portsmouth's delay.

39
00:03:05,660 --> 00:03:06,170
OK.

40
00:03:07,120 --> 00:03:14,110
Right, so once that is done, we would proceed, we wait for all the bids and we'll be stuck here and

41
00:03:14,110 --> 00:03:17,590
then until all the bids are set, then we'll proceed.

42
00:03:18,710 --> 00:03:25,150
So then we can use on if condition to check whether all the bits are set.

43
00:03:25,180 --> 00:03:31,900
So we come over here and we see if what is returned, which is our you x return and all bids Singh bids

44
00:03:31,900 --> 00:03:39,430
equals all being bids, meaning all of them said, then we can do something here.

45
00:03:41,420 --> 00:03:45,840
So basically, all of the tasks are synched, right?

46
00:03:45,980 --> 00:03:52,700
So this is going to be the same code for all of the other task, just that they will set their own bit

47
00:03:52,820 --> 00:03:53,510
and check.

48
00:03:54,410 --> 00:03:54,980
OK.

49
00:03:57,100 --> 00:03:59,950
So what I'm going to do is I'm going to make a copy of this.

50
00:04:01,200 --> 00:04:02,590
Going to make a copy of this.

51
00:04:06,100 --> 00:04:14,110
And call this task to an over here task to here will set its bit task, too, and wait for the rest.

52
00:04:15,970 --> 00:04:22,100
The same for TASC three, TASC three was set, it's a bit ago, this task three.

53
00:04:22,120 --> 00:04:28,210
First, it would set its bit and wait for the rest, but over here we can take the opportunity to print

54
00:04:28,210 --> 00:04:28,660
something.

55
00:04:29,020 --> 00:04:34,470
And the reason I don't want all of them to print something is because our UAT is a single resource.

56
00:04:34,480 --> 00:04:36,400
It requires exclusive access.

57
00:04:36,730 --> 00:04:44,590
If all the three task, they are synchronized, meaning they all execute what is in the if block at

58
00:04:44,590 --> 00:04:45,700
almost the same time.

59
00:04:45,850 --> 00:04:51,270
So if all of them are trying to print that, you are at almost the same time we know what would happen.

60
00:04:51,280 --> 00:04:53,470
We saw that in our semaphores experiment.

61
00:04:54,100 --> 00:04:55,060
So over here.

62
00:04:55,980 --> 00:04:57,720
I can print all task.

63
00:04:59,940 --> 00:05:00,450
Set.

64
00:05:03,810 --> 00:05:07,800
OK, and we'll find a controlled experiment to show what happens if this is not the case.

65
00:05:08,280 --> 00:05:08,730
OK.

66
00:05:11,980 --> 00:05:18,340
And what we have to do now is take the function prototypes to the top of the file, a copy task, one

67
00:05:18,340 --> 00:05:24,790
over here, and I'll drop this over here and then task to over here.

68
00:05:27,770 --> 00:05:30,940
And we have task number three as well.

69
00:05:33,440 --> 00:05:34,060
OK.

70
00:05:35,710 --> 00:05:36,780
And, um.

71
00:05:40,300 --> 00:05:40,810
Right.

72
00:05:40,840 --> 00:05:44,260
So let's see what we have we can click to build.

73
00:05:45,930 --> 00:05:47,340
I'll click over here to build.

74
00:05:51,700 --> 00:05:53,170
It's built successfully.

75
00:05:55,030 --> 00:05:57,190
And I'm going to right click over here.

76
00:05:58,110 --> 00:06:01,050
And go to the blog, US, ASTM 30 to.

77
00:06:02,170 --> 00:06:03,790
A select number 30.

78
00:06:05,640 --> 00:06:07,500
And then they'll say, OK, over here.

79
00:06:14,450 --> 00:06:15,680
And then I'll click to switch.

80
00:06:19,550 --> 00:06:22,970
This old project, I'm going to clean what we have over here.

81
00:06:23,000 --> 00:06:23,510
Claire.

82
00:06:29,050 --> 00:06:32,910
And then I'm going to open up several ports of barge trigger.

83
00:06:35,590 --> 00:06:37,630
This and I'll click to.

84
00:06:43,500 --> 00:06:44,790
All task set.

85
00:06:46,030 --> 00:06:46,480
Right.

86
00:06:47,090 --> 00:06:47,520
OK.

87
00:06:48,630 --> 00:06:51,330
So we were able to get all tasks set.

88
00:06:55,270 --> 00:06:55,840
Switch.

89
00:06:56,720 --> 00:07:00,380
I press this instead terminates and relaunch.

90
00:07:00,410 --> 00:07:04,010
That is why we've seen this works, but I want us to.

91
00:07:04,920 --> 00:07:08,070
Run this again, Cledus, and then Pressplay.

92
00:07:15,030 --> 00:07:16,100
It's rather strange.

93
00:07:16,380 --> 00:07:17,310
Let's stop and see.

94
00:07:21,150 --> 00:07:23,400
Events, bits, so let's stop this.

95
00:07:25,550 --> 00:07:26,600
And then, um.

96
00:07:28,830 --> 00:07:31,080
My computer is Lodin.

97
00:07:32,190 --> 00:07:33,270
Why do we have this?

98
00:07:35,310 --> 00:07:36,930
OK, real time is frozen.

99
00:07:39,000 --> 00:07:45,550
I'm going to add a bit of delay when we print to slow down and stabilize the print in a bit.

100
00:07:45,990 --> 00:07:47,010
So over here.

101
00:07:48,380 --> 00:07:54,370
I'm going to say delay for this amount and I'm going to add new line carriage return as well.

102
00:07:56,960 --> 00:07:59,420
OK, I'm going to click here to build.

103
00:08:09,590 --> 00:08:12,350
I see it build successfully as go to debugger.

104
00:08:17,200 --> 00:08:18,250
A click to switch.

105
00:08:23,110 --> 00:08:29,760
OK, I'll come over here, new line mode selector about right over here.

106
00:08:31,480 --> 00:08:33,640
And then select our ports over here.

107
00:08:36,960 --> 00:08:37,500
And then.

108
00:08:38,310 --> 00:08:44,490
I'll click to Open and I'll click to run over here, is going to see all tasks set.

109
00:08:44,840 --> 00:08:48,780
OK, so we are receiving all this.

110
00:08:48,780 --> 00:08:49,320
Looks good.

111
00:08:51,090 --> 00:08:53,550
So we've essentially synchronized the tasks.

112
00:08:57,750 --> 00:09:04,920
One way you can check this also is to have if you could if you can enable three LEDs, if you have the

113
00:09:04,920 --> 00:09:09,530
discovery board, you can enable three LEDs and place each of them in the tasks.

114
00:09:09,960 --> 00:09:17,790
And you could see the LED lights on when the when we reach here, the do something could simply be turning

115
00:09:17,790 --> 00:09:18,090
on on.

116
00:09:18,780 --> 00:09:22,410
And you should see all LEDs come on at once at the same time.

117
00:09:23,010 --> 00:09:23,550
OK.

118
00:09:25,510 --> 00:09:32,430
OK, so what is the controlled experiment, how do we know it is really sinking here and not just printing?

119
00:09:32,830 --> 00:09:40,150
OK, remember we said each task here, this function inherently makes the second argument.

120
00:09:40,150 --> 00:09:48,400
GetSet What I mean by this is when we call X Events group sync and we pass Task one Bethia, this sets

121
00:09:48,610 --> 00:09:50,240
the bits for task one bit.

122
00:09:50,440 --> 00:09:51,450
This sets this bit.

123
00:09:52,450 --> 00:09:58,000
So how we come into one of them and see if indeed we still have our message print.

124
00:09:58,360 --> 00:10:03,230
Right, because the idea is when we comment one of them task, one bit won't be set.

125
00:10:03,250 --> 00:10:07,820
So when we get here where the message is printed, it's waiting for all the bits.

126
00:10:08,410 --> 00:10:10,260
So we expect that task.

127
00:10:10,300 --> 00:10:11,320
One bit is not set.

128
00:10:11,320 --> 00:10:14,050
So waiting for all the bits should wait forever.

129
00:10:14,590 --> 00:10:21,280
So what I'm going to do is I'm going to highlight this and control to comment this out and I'm going

130
00:10:21,280 --> 00:10:22,240
to click here to build.

131
00:10:26,920 --> 00:10:31,900
It's built successfully, the one in is because we're not using this variable, but this is a controlled

132
00:10:31,900 --> 00:10:34,930
experiment, so we know that we click here.

133
00:10:40,450 --> 00:10:42,460
To go to debugger, click to switch.

134
00:10:46,490 --> 00:10:47,120
OK.

135
00:10:50,020 --> 00:10:51,220
So let's run.

136
00:10:51,250 --> 00:10:56,480
I'm going to press play over here, and as you can see, we have this system initializing.

137
00:10:56,860 --> 00:10:58,060
We've printed just this.

138
00:10:58,690 --> 00:11:02,230
And what is happening is that over here, they are all waiting.

139
00:11:03,290 --> 00:11:10,220
Task to task three, they are waiting for all the bits of bits to be synchronized, remember, all bits

140
00:11:10,220 --> 00:11:15,620
sink, they are waiting for all bits to be set and we know one of them is commented out, so we would

141
00:11:15,620 --> 00:11:16,430
never guess it.

142
00:11:16,640 --> 00:11:18,140
So we'll never have this printed.

143
00:11:18,320 --> 00:11:22,320
So this is what proves that indeed synchronisation is what is going on.

144
00:11:23,140 --> 00:11:27,170
OK, so we can comment what we've commented out.

145
00:11:29,880 --> 00:11:30,540
Over here.

146
00:11:34,960 --> 00:11:35,410
Right.

147
00:11:35,620 --> 00:11:41,020
So this is it, this is it for synchronizing tasks, using event groups.

148
00:11:41,290 --> 00:11:42,500
I'll see you the next lesson.

149
00:11:42,520 --> 00:11:43,330
Have a nice day.
