So guys, we had discussed that how asynchronous cancellation of the threads can lead to the problem
of resource leaking.
Now, in this lecture video, we will going to discuss that, how resource leaking can be 100% prevented
in asynchronous mode of cancellation of threads.
So if we could give a thread which is being canceled, one last chance to clean up all his mess.
Now here mess means the memory, the open file descriptors, the open files, etcetera.
Then resource leaking could be handled right.
So it does make sense if the thread that is being canceled is given one last chance so that he can clean
up all the resources that the thread has allocated, then resource leaking is prevented.
So Posix standards provide the concept of thread cleanup handlers.
Third, cleanup handlers are nothing, but they are special functions which are invoked just before
the threat is about to cancel.
Right.
So when the threat T is delivered, the cancel signal, then just before the threat is terminated or
canceled.
These threat cleanup handlers are invoked.
The threat cleanup handlers are a special functions, which is of this prototype.
That is the threat.
Cleanup handlers accept an argument which is of type Wordstar and they return a thing.
Right.
So this is actually a function pointer to the threat cleanup handler functions.
When cleanup handler function returns, that is, they complete their execution.
Then only after that, the threat is canceled immediately.
Right.
So what we will going to do is that we will going to extend our example, which we discussed last time.
We have a file master slave, one async cancellation dot C.
Now, in this file, we will going to implement this is the concept of threat cleanup handlers to derive
the file master slave one async cancellation cleanup handlers dot C Right.
So the second file is the solution which we obtained by implementing the thread cleanup handler solution
on the first file.
So we will going to look up and implement this file shortly.
But let us complete the theory part first.
So you can see in this slide that there is a thread function which is being executed in the context
of some thread, right?
There is some thread in the application which is doing its work by executing this function right.
So thread cleanup handlers are specified in the form of a stack and this stack is nothing but it is
a stack of functions.
Cleanup handlers are invoked from top of the stack to bottom of the stack.
So to set up the stack of thread cleanup functions.
In the thread function, which is a function executed in the context of a thread.
The very first thing that we need to do is to set up the stack of thread cleanup functions.
We do so by invoking an API thread cleanup push.
Right?
The first argument to this API is a pointer to the cleanup function and the second argument is a pointer
to the memory which will be supplied as an argument to this cleanup function when this cleanup function
will be invoked.
Right.
So as soon as this particular line is executed, a stack is created internally.
We call this stack as thread cancellation, clean up stack.
Right.
And the address of the function F one is pushed into this stack.
Similarly, you can push more clean up functions into this stack by invoking p thread, clean up push
API more number of times.
So here you can see that after function F one, the function f two has been pushed into the stack.
So function f two is at the top of the stack where function f one is at the bottom of the stack.
So now it simply means that suppose at point P your thread gets cancelled as soon as your thread receives
the cancelled signal.
The functions which are present in this stack would be invoked one after the another, starting from
the top of the stack towards the bottom of the stack.
So the function F2 will be invoked and then function f1 will be invoked and then the thread will be
cancelled.
Right.
So this is how this thread cancellation clean up stack work.
The functions which are pushed into this stack are invoked whenever your thread receives the cancellation
signal.
And in case if the thread do not receive the cancellation signal and executes to the completion successfully
without being cancelled, then it is the developer's responsibility to clean up this stack.
Right?
And when we say that we need to clean up the stack, it simply means that you need to pop out these
functions out of the stack.
So in order to pop out these functions, you need to invoke P thread cleanup pop API.
Right?
And you can pass zero as an argument to this API.
I will discuss shortly that what does this argument does, but in order to pop out the function from
the stack, you need to invoke this API with the argument.
Zero.
Right.
This API simply removes these functions from the stack.
In the last in first out order that is function f two will be removed first and then function f one
will be removed after that.
So pop the cleanup handlers from the stack.
If thread function do not cancel and execute to completion successfully.
Right.
But in case if your thread receives the cancellation signal, then these functions are automatically
invoked in the order in which they are specified in the stack from top to bottom as well as they will
be popped out from the stack on its own.
As a developer, you don't have to pop out these functions from the stack.
In case if your thread receives the cancellation signal right.
So now let us see a demo program in which we will going to exercise the concept of cleanup handlers.
So in the next lecture video, let us extend the functionality of our same program so that our threads
will now be able to handle resource leaking.