WEBVTT

00:06.990 --> 00:12.690
So guys, now continuing from the previous lecture video, let us take one more round of discussion

00:12.690 --> 00:16.050
discussing the difference between condition variables and mutex.

00:16.470 --> 00:21.540
So Mutex is used to grant an access to the resource if it is not locked already.

00:21.540 --> 00:22.260
Right.

00:22.620 --> 00:29.520
So now this statement is an obvious to us now condition Variables, on the other hand, allows thread

00:29.550 --> 00:31.920
to inspect the resource state.

00:31.950 --> 00:38.040
Right and decide if the thread wants to wait for favorable resource state.

00:38.070 --> 00:43.710
That is, if the resource is not in a position to be accessed by the thread, then in that situation

00:43.740 --> 00:51.300
a thread may choose to block itself and it will continue to be blocked until until the state of the

00:51.300 --> 00:56.130
resource is changed such that the thread can now access the resource.

00:56.310 --> 01:01.140
So let us try to discuss this point with the help of an example you can see on the screen.

01:01.140 --> 01:03.000
The screen is divided into two parts.

01:03.030 --> 01:04.140
The left hand side.

01:04.140 --> 01:07.330
I will try to explain Mutexes and the right hand side.

01:07.330 --> 01:12.790
I will try to explain condition variables and try to contrast the difference between mere access and

01:12.790 --> 01:14.050
condition variables.

01:14.050 --> 01:20.110
So going with the example mutex is all about granting an access to the laptop.

01:20.110 --> 01:24.370
For example, if the laptop is not being used by somebody else.

01:24.370 --> 01:25.090
Right.

01:25.090 --> 01:27.070
So this is as simple as that.

01:27.100 --> 01:32.650
Here the resource is the laptop and if the laptop is not being used by somebody else, then you can

01:32.650 --> 01:34.210
very well access the laptop.

01:34.540 --> 01:35.230
Condition.

01:35.230 --> 01:42.040
Variables, on the other hand, grants an access to the laptop only if it is not being used by somebody

01:42.040 --> 01:46.690
else and if it has an internet connection.

01:46.810 --> 01:52.690
So you can see in the case of condition variables, we have added an additional condition.

01:52.690 --> 01:53.440
Right?

01:55.420 --> 02:02.020
That is, you are allowed to access the laptop only and only if the laptop is not being used by somebody

02:02.020 --> 02:02.680
else.

02:02.710 --> 02:05.650
As well as the laptop has an internet connection.

02:05.890 --> 02:11.830
So you can see that the mutex only provides mutual exclusion, whereas condition variables provides

02:11.830 --> 02:17.190
mutual exclusion plus the custom condition based access to the user.

02:17.200 --> 02:17.950
Right.

02:18.310 --> 02:24.700
When you translate the mutexes and the condition variables usage into the code, you will see that the

02:24.700 --> 02:28.060
code on the left hand side demonstrates the use of mutexes.

02:28.060 --> 02:32.980
And now, while you have progressed this much into this course, you must be pretty much familiar with

02:32.980 --> 02:34.060
this piece of code.

02:34.210 --> 02:41.230
Once the thread t is able to grant a lock on the laptop murex the thread can perform any operation on

02:41.260 --> 02:42.100
that laptop.

02:42.100 --> 02:42.730
Right?

02:42.730 --> 02:44.770
So this is the critical section.

02:44.770 --> 02:52.780
Whereas on the other hand, in case of condition variables, a thread not only get an exclusive access

02:52.780 --> 02:59.270
on the laptop that is entered into the critical section, but also the thread checks whether the laptop

02:59.270 --> 03:01.370
has an internet connection or not.

03:01.490 --> 03:07.430
Now, in this case, if the laptop do not have an internet connection, the thread t decides to block

03:07.430 --> 03:08.180
itself.

03:08.180 --> 03:08.960
Right.

03:09.110 --> 03:15.020
So in the case of condition variable, you can see that we are using mutex as well as as well as the

03:15.020 --> 03:16.220
condition variable.

03:16.310 --> 03:19.880
The mutex helps in providing the mutual exclusion.

03:20.000 --> 03:23.840
That is, while the thread is inspecting the resource state.

03:23.870 --> 03:27.920
No other thread of the process should change the resource state.

03:27.920 --> 03:28.700
Right.

03:28.820 --> 03:36.650
So once the thread t gets a lock on the mutex, no other thread can change the state of internet connectivity

03:36.650 --> 03:37.730
of this laptop.

03:37.730 --> 03:38.480
Right.

03:38.480 --> 03:41.780
So it is for this reason that this mutex is required.

03:42.110 --> 03:47.840
Mutex helps the thread to exclusively check the resource state.

03:49.430 --> 03:52.520
Then the second part is the usage of this condition variable.

03:52.550 --> 03:57.840
This condition variables allows the calling thread to get blocked.

03:57.860 --> 04:04.160
If the thread do not find the resource in its favorable condition here, the favorable condition is

04:04.160 --> 04:07.130
that that the laptop should have an internet connection.

04:07.310 --> 04:14.030
But here if the thread t do not find the laptop having an internet connection, the thread can choose

04:14.030 --> 04:16.450
to block itself using condition variable.

04:16.460 --> 04:17.240
Right?

04:18.110 --> 04:24.170
So the whole essence here is that that the mutex only provides mutual exclusion, whereas condition

04:24.170 --> 04:31.430
variable is all about accessing the resource only when all the conditions which are in favor of providing

04:31.430 --> 04:34.340
the access of the resource to the thread are met.

04:35.150 --> 04:39.970
So note here is that that condition variable cannot be used without Mirax.

04:39.980 --> 04:43.310
There should be a paired mutex with the condition variable.

04:43.310 --> 04:44.090
Right.

04:44.820 --> 04:51.150
In other words, condition variable and mutex is a combo to be used together always.

04:51.450 --> 04:57.750
You need a mutual exclusion using mutex so that while at the time of testing the state of the resource,

04:57.780 --> 05:02.610
no other thread of the process changes the state of the resource.

05:02.640 --> 05:08.400
So summarizing the points in the condition variables condition variables are not used for mutual exclusion,

05:08.400 --> 05:10.320
but they are used for coordination.

05:10.320 --> 05:13.050
That is the coordination between the threads.

05:13.410 --> 05:16.170
So this is what we were going to discuss next.

05:16.410 --> 05:20.940
First, let us understand the basics of condition variables and how they are used.

05:20.970 --> 05:25.140
Then we will deep dive into the advanced applications of condition variables.

05:25.140 --> 05:31.800
So to put it simple, using condition variables, a thread can block itself if certain condition is

05:31.800 --> 05:32.610
not met.

05:32.610 --> 05:33.210
Right?

05:33.240 --> 05:36.690
For example, laptop not having an internet connectivity.

05:36.690 --> 05:42.810
So the API used by a thread to block itself using condition variable is p thread condition.

05:42.810 --> 05:43.500
Wait.

05:43.500 --> 05:44.250
Right.

05:44.940 --> 05:46.560
And using condition variables.

05:46.590 --> 05:52.250
A thread can signal another already blocked thread on a condition variable to resume.

05:52.260 --> 05:56.110
For that, the thread has to use an API thread condition signal.

05:56.130 --> 06:02.820
So if you have a thread t1 and in the execution flow of the thread t1 the thread t1 choose to block

06:02.820 --> 06:05.850
at the point p and to block at the point p.

06:05.880 --> 06:08.370
The thread uses an api p thread condition.

06:08.370 --> 06:09.070
Wait.

06:09.090 --> 06:11.640
So for short form I am just writing.

06:11.640 --> 06:12.240
Wait.

06:12.240 --> 06:12.960
Right.

06:13.560 --> 06:19.680
And while the threat even is blocked at the point P because the threat T one has invoked p thread condition

06:19.680 --> 06:27.210
wait call the other thread of the process during the course of its execution may choose at some random

06:27.210 --> 06:36.420
point Q to send a signal to the thread t one and when this signal is received by the thread t one the

06:36.420 --> 06:39.610
thread t one then resumes its execution.

06:39.630 --> 06:40.370
Right?

06:40.380 --> 06:44.970
So this signal is sent using this api p thread condition signal.

06:45.720 --> 06:50.760
So in the next lecture video we will going to discuss the concept of wait and signals.

06:50.760 --> 06:56.910
And these wait and signals are nothing, but these are the end result of invention of condition variables.

06:56.910 --> 06:57.660
Right?

06:57.930 --> 07:06.540
Using wait the thread block itself and using signals one thread unblocks the other already blocked thread.
