WEBVTT

00:04.900 --> 00:10.000
So guys, now let us make ourselves familiar with the very fundamental problem to multithreading and

00:10.000 --> 00:13.970
thread synchronization, and that is the spurious wakeups.

00:13.990 --> 00:19.600
So when you design multithreading solutions or when you write Multithreading code, you need to take

00:19.600 --> 00:22.590
care that you do not cause spurious wakeup problem.

00:22.630 --> 00:29.080
So we will first discuss what spurious wakeup are and how it can be handled or it can be resolved.

00:29.410 --> 00:33.510
It is the programmers responsibility to avoid spurious wakeup.

00:33.520 --> 00:38.880
As such, there is no API or no system call which can avoid spurious wakeup.

00:38.890 --> 00:44.780
It's a logical problem which a developer of a multithreading applications must handle.

00:44.800 --> 00:47.590
So a spurious wakeup is all about that.

00:47.590 --> 00:53.920
You wake up only to find whatever you were promised or told has been broken or is a lie.

00:54.220 --> 00:55.000
Right?

00:55.700 --> 00:59.120
So probably this statement make less sense to you as of now.

00:59.120 --> 01:01.790
So let us discuss with the help of our example.

01:02.120 --> 01:08.480
So let us suppose that you receive a phone call from your friend and he invites you for a booze party

01:08.480 --> 01:09.980
at a common friend's house.

01:10.010 --> 01:10.730
Right?

01:10.730 --> 01:17.420
So common friend is a third friend where you guys are planning to get together while you are on the

01:17.420 --> 01:18.080
way.

01:18.080 --> 01:24.130
The common friend leaves the town out of some emergency and he did not even inform you.

01:24.140 --> 01:31.090
So what will happen that you will eventually reach his home only to find that the common friend is nowhere?

01:31.100 --> 01:31.790
Right.

01:31.790 --> 01:35.600
So this is a situation of spurious wakeup to you.

01:38.340 --> 01:40.290
You eventually find the truth.

01:40.290 --> 01:43.590
And the truth is no less surprise to you.

01:43.950 --> 01:49.800
It takes finite amount of time for you to reach your friend's house, and within that time interval,

01:49.800 --> 01:55.860
the situation changed such that it was futile for you to reach your friend's house eventually.

01:55.860 --> 01:56.430
Right?

01:57.400 --> 02:01.390
Let us take one more example which demonstrates the spurious wakeup.

02:01.510 --> 02:02.650
Your father.

02:02.680 --> 02:09.730
Let us suppose bought sweets, which is exclusive for you, and he advised you to consume those sweets

02:09.730 --> 02:11.080
after taking a bath.

02:11.110 --> 02:16.090
So you go for a bath and your sibling is somewhat notorious guy.

02:16.090 --> 02:22.690
He overheard this conversation between you and your father, and he eat all the sweets in the box,

02:22.690 --> 02:24.130
leaving the box empty.

02:24.160 --> 02:24.940
Right?

02:25.030 --> 02:32.260
Now you come out of the washroom after taking a bath and you open the sweet box only to find that the

02:32.260 --> 02:33.750
sweet box is empty.

02:33.760 --> 02:34.450
Right.

02:34.450 --> 02:35.500
So note the word.

02:35.500 --> 02:39.070
It is something against your expectation in the previous example.

02:39.070 --> 02:45.790
Also you reach your friend's house only to find that the friend was not at his house.

02:45.790 --> 02:49.590
So it was also something that was against your expectation.

02:49.600 --> 02:56.230
So in the second example also when you find the sweet box empty, then it was the spurious wakeup for

02:56.230 --> 02:56.930
you.

02:57.410 --> 03:00.410
So as a result, you are sad and angry.

03:00.420 --> 03:05.930
So when you wake up spuriously you find that the situation is against your expectations.

03:05.930 --> 03:12.620
In both of these examples, you ended up with a situation which was against your expectations, right?

03:12.920 --> 03:20.000
You think you have been given wrong information by your friend in the first example or by your father

03:20.000 --> 03:24.230
in the second example, but actually they did not give you wrong information.

03:24.230 --> 03:28.550
It just happened that somebody else cheated you.

03:28.550 --> 03:29.180
Right.

03:29.180 --> 03:35.870
In both of these examples, in the first example, the common friend cheated you knowingly or unknowingly,

03:35.870 --> 03:38.960
but your friend gave you the right information.

03:38.960 --> 03:39.620
Right?

03:39.650 --> 03:42.950
Similarly, in the second example, your father never cheated you.

03:42.980 --> 03:47.390
He gave you the right information to have a sweets in the sweet box.

03:47.390 --> 03:52.340
But it so happened that you eventually find the sweet box empty.

03:52.340 --> 03:55.640
And you think that your father lied to you, right?

03:55.640 --> 03:57.050
So now let us see that.

03:57.050 --> 04:01.580
How this spurious wakeup is relevant in the world of Multithreading.

04:02.190 --> 04:09.420
So when it comes to multithreading, when a thread gets unblocked, that is the thread which was blocked

04:09.450 --> 04:11.110
on the wait system call.

04:11.130 --> 04:18.240
So when the thread gets unblocked because it has received a signal, it can resume its execution due

04:18.240 --> 04:21.140
to a reason which is no more valid.

04:21.150 --> 04:21.810
Right.

04:21.810 --> 04:27.090
So in the next lecture video we will going to understand this point with the help of an example, the

04:27.090 --> 04:31.100
thread blocks because some condition was not fulfilled.

04:31.110 --> 04:38.670
But when a thread receives a signal and starts its execution, that condition, because of which it

04:38.670 --> 04:41.740
was blocked, is still not fulfilled.

04:41.760 --> 04:44.970
That is called spurious wakeup for a thread.

04:46.270 --> 04:53.110
So now let us see in the next lecture video how threads can be subjected to spurious wakeups and what

04:53.110 --> 04:58.960
shall be the problem that would happen if the threads are subjected to spurious wakeups?

04:58.960 --> 05:05.170
And as a developer or programmer, what you should do in order to prevent spurious wakeups of blocked

05:05.200 --> 05:06.040
threads.
