So guys, now continuing from the previous lecture video, let us take one more round of discussion
discussing the difference between condition variables and mutex.
So Mutex is used to grant an access to the resource if it is not locked already.
Right.
So now this statement is an obvious to us now condition Variables, on the other hand, allows thread
to inspect the resource state.
Right and decide if the thread wants to wait for favorable resource state.
That is, if the resource is not in a position to be accessed by the thread, then in that situation
a thread may choose to block itself and it will continue to be blocked until until the state of the
resource is changed such that the thread can now access the resource.
So let us try to discuss this point with the help of an example you can see on the screen.
The screen is divided into two parts.
The left hand side.
I will try to explain Mutexes and the right hand side.
I will try to explain condition variables and try to contrast the difference between mere access and
condition variables.
So going with the example mutex is all about granting an access to the laptop.
For example, if the laptop is not being used by somebody else.
Right.
So this is as simple as that.
Here the resource is the laptop and if the laptop is not being used by somebody else, then you can
very well access the laptop.
Condition.
Variables, on the other hand, grants an access to the laptop only if it is not being used by somebody
else and if it has an internet connection.
So you can see in the case of condition variables, we have added an additional condition.
Right?
That is, you are allowed to access the laptop only and only if the laptop is not being used by somebody
else.
As well as the laptop has an internet connection.
So you can see that the mutex only provides mutual exclusion, whereas condition variables provides
mutual exclusion plus the custom condition based access to the user.
Right.
When you translate the mutexes and the condition variables usage into the code, you will see that the
code on the left hand side demonstrates the use of mutexes.
And now, while you have progressed this much into this course, you must be pretty much familiar with
this piece of code.
Once the thread t is able to grant a lock on the laptop murex the thread can perform any operation on
that laptop.
Right?
So this is the critical section.
Whereas on the other hand, in case of condition variables, a thread not only get an exclusive access
on the laptop that is entered into the critical section, but also the thread checks whether the laptop
has an internet connection or not.
Now, in this case, if the laptop do not have an internet connection, the thread t decides to block
itself.
Right.
So in the case of condition variable, you can see that we are using mutex as well as as well as the
condition variable.
The mutex helps in providing the mutual exclusion.
That is, while the thread is inspecting the resource state.
No other thread of the process should change the resource state.
Right.
So once the thread t gets a lock on the mutex, no other thread can change the state of internet connectivity
of this laptop.
Right.
So it is for this reason that this mutex is required.
Mutex helps the thread to exclusively check the resource state.
Then the second part is the usage of this condition variable.
This condition variables allows the calling thread to get blocked.
If the thread do not find the resource in its favorable condition here, the favorable condition is
that that the laptop should have an internet connection.
But here if the thread t do not find the laptop having an internet connection, the thread can choose
to block itself using condition variable.
Right?
So the whole essence here is that that the mutex only provides mutual exclusion, whereas condition
variable is all about accessing the resource only when all the conditions which are in favor of providing
the access of the resource to the thread are met.
So note here is that that condition variable cannot be used without Mirax.
There should be a paired mutex with the condition variable.
Right.
In other words, condition variable and mutex is a combo to be used together always.
You need a mutual exclusion using mutex so that while at the time of testing the state of the resource,
no other thread of the process changes the state of the resource.
So summarizing the points in the condition variables condition variables are not used for mutual exclusion,
but they are used for coordination.
That is the coordination between the threads.
So this is what we were going to discuss next.
First, let us understand the basics of condition variables and how they are used.
Then we will deep dive into the advanced applications of condition variables.
So to put it simple, using condition variables, a thread can block itself if certain condition is
not met.
Right?
For example, laptop not having an internet connectivity.
So the API used by a thread to block itself using condition variable is p thread condition.
Wait.
Right.
And using condition variables.
A thread can signal another already blocked thread on a condition variable to resume.
For that, the thread has to use an API thread condition signal.
So if you have a thread t1 and in the execution flow of the thread t1 the thread t1 choose to block
at the point p and to block at the point p.
The thread uses an api p thread condition.
Wait.
So for short form I am just writing.
Wait.
Right.
And while the threat even is blocked at the point P because the threat T one has invoked p thread condition
wait call the other thread of the process during the course of its execution may choose at some random
point Q to send a signal to the thread t one and when this signal is received by the thread t one the
thread t one then resumes its execution.
Right?
So this signal is sent using this api p thread condition signal.
So in the next lecture video we will going to discuss the concept of wait and signals.
And these wait and signals are nothing, but these are the end result of invention of condition variables.
Right?
Using wait the thread block itself and using signals one thread unblocks the other already blocked thread.