WEBVTT

00:07.090 --> 00:12.700
So, guys, now in this lecture, we will we will going to discuss how to avoid spurious wake up when

00:12.700 --> 00:14.790
we have multiple conditions to deal with.

00:15.790 --> 00:22.480
So women are so familiar with the concept of a spurious wake up and how to handle it by simply replacing

00:22.480 --> 00:24.490
the IFA statement with the wild condition.

00:25.330 --> 00:27.640
All right now, you must be very much familiar with this.

00:28.150 --> 00:31.180
So now we will going to discuss a little bit of extension of it.

00:31.960 --> 00:37.840
So consider that you want to go to the party and let us say that going to the party is some kind of

00:37.840 --> 00:38.800
a critical section.

00:39.490 --> 00:39.850
All right.

00:40.360 --> 00:46.120
And you only go to the party only and only when the following conditions are met.

00:46.720 --> 00:49.240
The condition one is that it must not be raining.

00:49.750 --> 00:55.990
Condition to say is that your best friend must be in the town and condition, three says, is that you

00:55.990 --> 01:03.340
must have a light available so you are willing to go to the party only and only when all these three

01:03.340 --> 01:06.610
conditions combined are satisfied.

01:08.350 --> 01:08.710
All right.

01:09.070 --> 01:18.370
So let us see how this code can be translated to the equivalent piece of code in which a triad is supposed

01:18.370 --> 01:22.180
to get blocked if all these three conditions are not satisfied.

01:23.320 --> 01:29.050
So let's say we have some Bartomeu decks, so we were going to stand with all the conditions to be tested

01:29.050 --> 01:31.390
in between mutex unlock and mutex lock.

01:32.170 --> 01:33.290
Now we very well know.

01:33.310 --> 01:39.250
Why do we sandwich all the conditions to be tested within mutex, lock and mutex some locks?

01:40.210 --> 01:48.250
So now let's say that you test the condition number one and condition number one is that it must not

01:48.250 --> 01:48.790
be raining.

01:49.690 --> 01:52.720
So let's say that this condition comes out to be false.

01:52.840 --> 01:54.580
It means that it is raining.

01:55.270 --> 01:56.260
So what will you do?

01:56.320 --> 01:58.780
You will go and sleep in your bed.

01:59.350 --> 02:04.660
I have intensely choosing that you go and sleep in a bath so that you lie unconscious.

02:05.290 --> 02:11.080
You yourself cannot wake up and look outside the window of your room and see whether it is still raining

02:11.080 --> 02:11.590
or not.

02:12.040 --> 02:13.350
You are sleeping in your bed.

02:13.360 --> 02:14.470
You are unconscious.

02:15.010 --> 02:17.860
Sleeping in the bed represents that.

02:17.860 --> 02:23.230
The threat is in the blog to state which by itself cannot do any kind of inspection.

02:24.870 --> 02:29.400
So let's say after some time, your sovereign comes and he wakes you up.

02:30.330 --> 02:36.600
So coming of the sovereign and waking you up means that some of the threat in the system has sent a

02:36.600 --> 02:38.100
signal to the blocked threat.

02:39.060 --> 02:39.420
All right.

02:39.780 --> 02:47.340
So you wake up, and since you are captured within the wild block, you will go and test the condition

02:47.340 --> 02:47.730
again.

02:48.720 --> 02:55.680
And let's say that it is still raining so you go and sleep in the bed again, and after some time,

02:55.680 --> 02:59.650
your or someone comes again wakes you up and the cycle goes on.

03:00.750 --> 03:07.500
So let's say that at some point of time you whined that when you look outside the window, it does not

03:07.500 --> 03:07.920
raining.

03:08.430 --> 03:14.610
So now you are happy, and now you jump to the condition number two condition number two say, is that

03:14.610 --> 03:17.370
your best friend is in the town or not?

03:18.270 --> 03:21.800
So now let's say that your best friend is not in the town.

03:22.050 --> 03:25.140
Therefore, you are going to decide to go to sleep in the bad.

03:26.270 --> 03:28.220
Your cell phone comes, wakes you up.

03:28.730 --> 03:34.670
You again go and test or check whether your best friend has arrived in the town or not.

03:35.510 --> 03:37.100
So let's say your best friend has now.

03:37.130 --> 03:37.760
All right.

03:38.480 --> 03:45.320
So you are happy and you go ahead and test the condition number three and condition number three.

03:45.350 --> 03:47.780
Let's say that the girl is not available to you.

03:48.320 --> 03:54.170
So you decide to sleep again in the bad, and let's hear someone come and he wakes you up.

03:54.740 --> 03:58.460
So again, you check whether the vehicle is available or not.

03:59.150 --> 04:01.700
And let's say that now the vehicle is available.

04:02.570 --> 04:02.960
All right.

04:03.350 --> 04:07.580
So now you are happy and you decide to go to the party.

04:07.580 --> 04:08.960
But there is one flaw in that.

04:09.800 --> 04:15.440
By the time you check that the vehicle is available, it might be possible that it started raining again.

04:16.800 --> 04:17.190
All right.

04:17.850 --> 04:18.870
So it means that.

04:20.040 --> 04:26.610
As soon as you come to know that the WAKIL is available, you must also check whether it has started

04:26.610 --> 04:27.660
raining again or not.

04:28.290 --> 04:34.080
And you must also check whether your friend is still in the town or not, then only you should go to

04:34.080 --> 04:34.650
the party.

04:36.000 --> 04:42.330
So the thing here is that that every time the tribe wakes up, it must check all the conditions to a

04:42.330 --> 04:43.650
wide, spurious wake up.

04:43.650 --> 04:46.050
All conditions combined should be tested.

04:47.480 --> 04:54.170
So this piece of code is actually a wrong way for testing the conditions to avoid spurious makeup.

04:54.830 --> 05:00.860
In this piece of code, we are checking three different conditions but not combined together, and this

05:00.860 --> 05:02.150
will be the source of error.

05:03.260 --> 05:06.950
So in the next slide, the same code can be rewritten as follows.

05:07.730 --> 05:10.170
And this is the correct code for testing the conditions.

05:10.910 --> 05:17.330
As soon as you wake up while sleeping in the bed at any of these three stages, you will go back to

05:17.330 --> 05:23.690
the first condition and test again from the beginning, and you will end up going to the party only

05:23.690 --> 05:29.660
when you are sure that all the three conditions for going to the party is satisfied.

05:30.710 --> 05:36.860
So every time you sleep from the batch, you go to the beginning and test all the conditions again.

05:38.390 --> 05:38.810
All right.

05:39.260 --> 05:48.110
So the very sunset or precise way of writing such piece of code is that that all the conditions must

05:48.110 --> 05:54.830
be tested along with their negations with the author or logical operator between them.

05:56.090 --> 06:02.420
So to avoid a spurious wake up, you must write, you must test all the conditions combined together

06:02.420 --> 06:04.880
in this way within a single one statement.

06:05.360 --> 06:12.200
And if it is not possible to express checking all of the conditions in a single one statement, because

06:12.200 --> 06:18.290
your conditions may be complicated so you can translate the piece of code on the right hand side into

06:18.590 --> 06:21.050
a little expanded or lengthy piece of code.

06:21.050 --> 06:24.800
On the left hand side, both the codes are logically equivalent.

06:26.180 --> 06:32.030
So this is a common error that most of the people do while writing multithreaded code always check the

06:32.030 --> 06:35.240
conditions combined together within the same wild statement.

06:35.660 --> 06:40.070
And this would ensure that spurious make up do not happen at all.
