WEBVTT

00:05.230 --> 00:11.530
So, guys, now next we will going to discuss the mutex locking so the way we lock and unlock the mutex

00:11.530 --> 00:14.870
can be divided or can be categorized into two categories.

00:14.890 --> 00:20.110
The first category is called code locking, and the second category is called object locking.

00:20.230 --> 00:25.690
So let us discuss these two ways of locking and unlocking the mutex in a little bit detail.

00:26.050 --> 00:33.700
So if you need to protect a code snippet, that is a portion of the code of your program against concurrent

00:33.730 --> 00:37.300
thread, unsafe access, then we will go for code locking.

00:37.390 --> 00:44.800
And if you need to protect an object against concurrent thread unsafe access, then we go for object

00:44.800 --> 00:45.460
locking.

00:45.460 --> 00:46.060
Right?

00:46.060 --> 00:51.760
So at this point of time, these two lines may not make much sense to you, so let us discuss the code

00:51.760 --> 00:54.490
locking and object locking with the help of an example.

00:55.450 --> 00:58.000
So let us discuss the code locking first.

00:58.030 --> 01:06.460
So let us suppose that you have a file dot C, and in this file you have taken a mutex object which

01:06.460 --> 01:07.960
is global within this file.

01:07.960 --> 01:08.560
Right?

01:08.590 --> 01:14.950
The scope of this object is within this file because the declaration of this object is guarded by this

01:14.950 --> 01:16.000
static keyword.

01:16.000 --> 01:16.690
Right?

01:17.770 --> 01:22.210
Now, the question is that why we have taken this mutex global variable in this file?

01:22.870 --> 01:29.350
The answer is that that using this global mutex object, we can protect all the critical sections which

01:29.350 --> 01:30.690
are present in this file.

01:30.700 --> 01:31.360
Right?

01:31.360 --> 01:34.900
So, for example, this is the critical section in this file.

01:34.900 --> 01:37.150
There could be more critical section in this file.

01:37.180 --> 01:42.910
But for example, let us consider that there is only one critical section in this file in the function

01:42.910 --> 01:50.380
foo and we guard or protect this critical section against concurrent unsafe thread access by sandwiching

01:50.380 --> 01:53.650
this critical section between mutex, lock and mutex.

01:53.650 --> 01:55.580
Unlock APIs right.

01:56.780 --> 02:02.210
So it would simply means that at most one thread can execute inside this critical section.

02:02.240 --> 02:06.500
Now, why we are saying that this type of locking is a code locking.

02:06.530 --> 02:12.800
The reason is that that just by looking at the code as a programmer or developer, you can very well

02:12.800 --> 02:17.630
identify that this particular region of the code is a critical section.

02:17.840 --> 02:21.830
So in code locking mutex are defined in the source file level.

02:21.830 --> 02:27.440
That is, we have this mutex which is defined as a global variable within this file and we use this

02:27.440 --> 02:30.320
mutex object to protect all the critical sections.

02:30.320 --> 02:36.080
So let us take an example to understand this code locking so you can see the code on the right hand

02:36.110 --> 02:43.580
side, we have a global buffer, which is a global object or global variable within this file, right?

02:45.250 --> 02:50.830
And let us suppose that inside this file you have taken a global mutex object, right?

02:51.520 --> 02:57.640
Now, if you analyze this function packet received, you will see that in this function we are making

02:57.640 --> 03:02.380
use of this global variable that is global buffer, right?

03:02.380 --> 03:07.990
So it simply means that these three lines of code of this function is a critical section.

03:08.140 --> 03:15.160
If this function that is packet receive is accessed by thread t one thread t two, then this global

03:15.160 --> 03:21.820
buffer will be written and accessed in a thread unsafe manner by the two threads t1 and t2 thread.

03:21.820 --> 03:28.450
Unsafe manner means the thread t one and t two will perform write write operation over this global buffer

03:28.450 --> 03:30.760
which are conflicting operations.

03:32.430 --> 03:38.430
So here you can see that as a programmer or developer, just by looking at the code, you can very well

03:38.430 --> 03:43.350
say that these three lines forms the critical section, right?

03:43.980 --> 03:50.250
So it would simply means that you need to sandwich these three lines of code between pthread mutex lock

03:50.250 --> 03:51.420
and pthread mutex.

03:51.420 --> 03:52.680
Unlock calls.

03:53.590 --> 04:00.760
So when you use a mutex to actually protect a code fragment, we call this type of locking as code locking.

04:00.760 --> 04:01.480
Right?
