So, guys, now let us discuss the point number three.
That is thread termination.
It simply means that when a new thread is created, then in how many ways a new thread can be terminated.
So there are three ways in which a thread can be terminated.
A thread can simply terminate if its thread function returns.
That is, its thread function runs to completion successfully.
Right.
A thread can also be terminated if it invokes an api p thread underscore exit.
Right.
And the third way of terminating a thread is that if some other thread kills or cancels the thread.
Right.
So the third point refers to as thread cancellation.
And we were going to discuss this thread cancellation in pretty much detail in the future section of
this course.
So now through our Hello World example, let us discuss the first two points.
That is when the thread function returns and if the thread invokes an api p thread exit.
So in our Hello world example our child thread actually never terminates because it was executing in
the while loop, which is an infinite loop.
Right.
So if simply change the condition here, let's say I take a variable int a is equal to zero.
And here if I simply check that, execute as long as a is less than ten.
Right.
So now our child thread is not immortal and it will going to end in some time.
Right?
In fact, in 10s our child thread function will going to terminate.
Now why the child thread terminate because its thread function would end right?
It would return.
So this is one way how a thread terminate.
The second way is that that the child thread during its execution invokes an API thread underscore exit.
So here if I simply type out that if a is equal equal five, then I want to terminate my thread.
So what I'm going to do is that simply invoke an API thread.
Exit zero.
So this is one way in which my child thread gets terminated using an API thread.
Underscore exit.
Right.
Now what does that zero represent in the bracket that we will going to discuss in detail in the subsequent
section of this course.
So you can make these little changes in your Hello World program that you have written and try out.
Coming to the third point, that is thread cancellation.
So here thread cancellation simply means that one thread is canceled or terminated by other thread.
So in this example you can say that the thread t one creates a thread t two at the fourth point f right.
Now let us suppose that the thread T2 is doing its work and it is somewhere doing its work at point
B.
Point B is some instruction.
Point B is some instruction in the execution flow of the thread.
T2 right.
Now thread t1 wishes to terminate the thread t2 right.
So thread t1 at any point of time can send the cancel request to the thread t2 right.
When this cancel request is received by the thread t2 it don't matter what thread t2 was busy with,
it will get terminated right.
Now note that it is not necessary that it is only the parent thread which can cancel child thread.
Even child thread can also cancel the parent thread.
Right.
So any thread can cancel any other thread.
In fact, one thread can cancel many other threads.
Right.
So it all depends.
Sending this cancellation request to the thread.
A thread which receives this cancellation request will get cancelled.
So we will discuss in detail regarding thread cancellation in the future section of this course.
For now I have just given an overview that cancellation of the thread is the third way in which a thread
can be cancelled or terminated.