So, guys, now in this lecture, we will we will going to discuss how to avoid spurious wake up when
we have multiple conditions to deal with.
So women are so familiar with the concept of a spurious wake up and how to handle it by simply replacing
the IFA statement with the wild condition.
All right now, you must be very much familiar with this.
So now we will going to discuss a little bit of extension of it.
So consider that you want to go to the party and let us say that going to the party is some kind of
a critical section.
All right.
And you only go to the party only and only when the following conditions are met.
The condition one is that it must not be raining.
Condition to say is that your best friend must be in the town and condition, three says, is that you
must have a light available so you are willing to go to the party only and only when all these three
conditions combined are satisfied.
All right.
So let us see how this code can be translated to the equivalent piece of code in which a triad is supposed
to get blocked if all these three conditions are not satisfied.
So let's say we have some Bartomeu decks, so we were going to stand with all the conditions to be tested
in between mutex unlock and mutex lock.
Now we very well know.
Why do we sandwich all the conditions to be tested within mutex, lock and mutex some locks?
So now let's say that you test the condition number one and condition number one is that it must not
be raining.
So let's say that this condition comes out to be false.
It means that it is raining.
So what will you do?
You will go and sleep in your bed.
I have intensely choosing that you go and sleep in a bath so that you lie unconscious.
You yourself cannot wake up and look outside the window of your room and see whether it is still raining
or not.
You are sleeping in your bed.
You are unconscious.
Sleeping in the bed represents that.
The threat is in the blog to state which by itself cannot do any kind of inspection.
So let's say after some time, your sovereign comes and he wakes you up.
So coming of the sovereign and waking you up means that some of the threat in the system has sent a
signal to the blocked threat.
All right.
So you wake up, and since you are captured within the wild block, you will go and test the condition
again.
And let's say that it is still raining so you go and sleep in the bed again, and after some time,
your or someone comes again wakes you up and the cycle goes on.
So let's say that at some point of time you whined that when you look outside the window, it does not
raining.
So now you are happy, and now you jump to the condition number two condition number two say, is that
your best friend is in the town or not?
So now let's say that your best friend is not in the town.
Therefore, you are going to decide to go to sleep in the bad.
Your cell phone comes, wakes you up.
You again go and test or check whether your best friend has arrived in the town or not.
So let's say your best friend has now.
All right.
So you are happy and you go ahead and test the condition number three and condition number three.
Let's say that the girl is not available to you.
So you decide to sleep again in the bad, and let's hear someone come and he wakes you up.
So again, you check whether the vehicle is available or not.
And let's say that now the vehicle is available.
All right.
So now you are happy and you decide to go to the party.
But there is one flaw in that.
By the time you check that the vehicle is available, it might be possible that it started raining again.
All right.
So it means that.
As soon as you come to know that the WAKIL is available, you must also check whether it has started
raining again or not.
And you must also check whether your friend is still in the town or not, then only you should go to
the party.
So the thing here is that that every time the tribe wakes up, it must check all the conditions to a
wide, spurious wake up.
All conditions combined should be tested.
So this piece of code is actually a wrong way for testing the conditions to avoid spurious makeup.
In this piece of code, we are checking three different conditions but not combined together, and this
will be the source of error.
So in the next slide, the same code can be rewritten as follows.
And this is the correct code for testing the conditions.
As soon as you wake up while sleeping in the bed at any of these three stages, you will go back to
the first condition and test again from the beginning, and you will end up going to the party only
when you are sure that all the three conditions for going to the party is satisfied.
So every time you sleep from the batch, you go to the beginning and test all the conditions again.
All right.
So the very sunset or precise way of writing such piece of code is that that all the conditions must
be tested along with their negations with the author or logical operator between them.
So to avoid a spurious wake up, you must write, you must test all the conditions combined together
in this way within a single one statement.
And if it is not possible to express checking all of the conditions in a single one statement, because
your conditions may be complicated so you can translate the piece of code on the right hand side into
a little expanded or lengthy piece of code.
On the left hand side, both the codes are logically equivalent.
So this is a common error that most of the people do while writing multithreaded code always check the
conditions combined together within the same wild statement.
And this would ensure that spurious make up do not happen at all.