So, guys, now next we will going to discuss the mutex locking so the way we lock and unlock the mutex
can be divided or can be categorized into two categories.
The first category is called code locking, and the second category is called object locking.
So let us discuss these two ways of locking and unlocking the mutex in a little bit detail.
So if you need to protect a code snippet, that is a portion of the code of your program against concurrent
thread, unsafe access, then we will go for code locking.
And if you need to protect an object against concurrent thread unsafe access, then we go for object
locking.
Right?
So at this point of time, these two lines may not make much sense to you, so let us discuss the code
locking and object locking with the help of an example.
So let us discuss the code locking first.
So let us suppose that you have a file dot C, and in this file you have taken a mutex object which
is global within this file.
Right?
The scope of this object is within this file because the declaration of this object is guarded by this
static keyword.
Right?
Now, the question is that why we have taken this mutex global variable in this file?
The answer is that that using this global mutex object, we can protect all the critical sections which
are present in this file.
Right?
So, for example, this is the critical section in this file.
There could be more critical section in this file.
But for example, let us consider that there is only one critical section in this file in the function
foo and we guard or protect this critical section against concurrent unsafe thread access by sandwiching
this critical section between mutex, lock and mutex.
Unlock APIs right.
So it would simply means that at most one thread can execute inside this critical section.
Now, why we are saying that this type of locking is a code locking.
The reason is that that just by looking at the code as a programmer or developer, you can very well
identify that this particular region of the code is a critical section.
So in code locking mutex are defined in the source file level.
That is, we have this mutex which is defined as a global variable within this file and we use this
mutex object to protect all the critical sections.
So let us take an example to understand this code locking so you can see the code on the right hand
side, we have a global buffer, which is a global object or global variable within this file, right?
And let us suppose that inside this file you have taken a global mutex object, right?
Now, if you analyze this function packet received, you will see that in this function we are making
use of this global variable that is global buffer, right?
So it simply means that these three lines of code of this function is a critical section.
If this function that is packet receive is accessed by thread t one thread t two, then this global
buffer will be written and accessed in a thread unsafe manner by the two threads t1 and t2 thread.
Unsafe manner means the thread t one and t two will perform write write operation over this global buffer
which are conflicting operations.
So here you can see that as a programmer or developer, just by looking at the code, you can very well
say that these three lines forms the critical section, right?
So it would simply means that you need to sandwich these three lines of code between pthread mutex lock
and pthread mutex.
Unlock calls.
So when you use a mutex to actually protect a code fragment, we call this type of locking as code locking.
Right?