So guys, in the previous lecture video, we discussed asynchronous mode of thread cancellation.
Now let us discuss what are the problems with asynchronous mode of thread cancellation.
Right.
So let me ask you one simple question.
What will happen if you are driving a car and suddenly you are asked to leave the steering wheel right?
Suddenly may God forbid you would encounter an accident.
The same applies here.
When we say that the thread is cancelled asynchronously, it simply means that the thread can be cancelled
at any point in its execution flow.
And when the thread gets cancelled at any arbitrary point in its execution flow, it can lead to certain
problems such as resource leaking, data structure, corruption.
We call this data structure corruption with a technical term called invariance, and it may also lead
to deadlocks.
So now let us understand these one, two, three points one by one, with the help of an example to
see that how asynchronous mode of cancellation of the threads can lead to these problems.
So let us discuss the resource leaking.
So if you notice the program that we just discussed and wrote in the last lecture video, so this is
the thread function, right?
And the thread which is executing in this function can be asynchronously cancelled right now.
If you notice this thread usually span its most of the time in between line 55 to 61.
Right.
Because our slave threads are executing in infinite loop, right?
So it simply means that when some other thread in our case it was the main thread send the cancelled
signal to this thread which is executing in this function.
The thread must be executing in any of these lines.
That is from line number 55 to 60.
Right.
The thread could be executing at any line within this while loop when the thread receives the cancel
signal.
Now the question is that that when the thread receives this cancelled signal, the thread gets terminated.
So do you see a problem here that when the thread gets terminated, our thread did not get the opportunity
to actually close this file pointer.
Right.
So it is causing a resource leak.
An open file is supposed to be closed.
Right.
Similarly, if you notice the argument of the thread was the thread ID and this argument was actually
a heap memory.
You can see that here was the thread ID that was allocated a memory, right.
So it simply means that when the thread got canceled, the thread did not get any chance to clean up
any memory which it was supposed to do.
So it was causing a memory leak.
Right.
A thread is canceled, but this memory pointer was not freed, so it caused a memory leak.
So this is the problem with respect to resource leaking that a thread which is being canceled in an
asynchronous mode may leave some open file descriptors or sockets open forever.
Right.
And we have also seen that in our example we end up with leaking the memory when our slave thread got
cancelled.
Right?
So this problem of resource leaking is addressed by using the concept of cleanup handlers.
Cleanup handlers is all about giving a thread one last chance before it gets canceled so that the thread
can clean up all the resources that it is supposed to clean.
Right.
Whereas the problem number two and three is handled by a concept of cancellation points.
We will discuss these cancellation points when we will discuss the default cancellation.
Right.
So now that I have explained the point, number one, that why asynchronous mode of cancellation of
the thread can subject to resource leaking.
Let us discuss in the next lecture video that how asynchronous mode of cancellation of the thread can
lead to data structure corruption or it can lead to deadlocks.