WEBVTT

00:05.010 --> 00:07.410
So, guys, now let us discuss the point number three.

00:07.410 --> 00:08.880
That is thread termination.

00:08.910 --> 00:14.940
It simply means that when a new thread is created, then in how many ways a new thread can be terminated.

00:15.300 --> 00:19.170
So there are three ways in which a thread can be terminated.

00:19.350 --> 00:23.190
A thread can simply terminate if its thread function returns.

00:23.190 --> 00:27.300
That is, its thread function runs to completion successfully.

00:27.300 --> 00:28.080
Right.

00:28.290 --> 00:33.300
A thread can also be terminated if it invokes an api p thread underscore exit.

00:33.300 --> 00:34.080
Right.

00:34.230 --> 00:41.580
And the third way of terminating a thread is that if some other thread kills or cancels the thread.

00:41.580 --> 00:42.180
Right.

00:42.180 --> 00:45.030
So the third point refers to as thread cancellation.

00:45.030 --> 00:49.710
And we were going to discuss this thread cancellation in pretty much detail in the future section of

00:49.710 --> 00:50.520
this course.

00:50.940 --> 00:54.630
So now through our Hello World example, let us discuss the first two points.

00:54.630 --> 01:00.360
That is when the thread function returns and if the thread invokes an api p thread exit.

01:00.810 --> 01:07.150
So in our Hello world example our child thread actually never terminates because it was executing in

01:07.150 --> 01:09.430
the while loop, which is an infinite loop.

01:09.430 --> 01:10.000
Right.

01:11.840 --> 01:19.220
So if simply change the condition here, let's say I take a variable int a is equal to zero.

01:19.220 --> 01:25.700
And here if I simply check that, execute as long as a is less than ten.

01:26.090 --> 01:27.050
Right.

01:27.260 --> 01:32.810
So now our child thread is not immortal and it will going to end in some time.

01:32.810 --> 01:33.400
Right?

01:33.410 --> 01:38.150
In fact, in 10s our child thread function will going to terminate.

01:38.240 --> 01:43.400
Now why the child thread terminate because its thread function would end right?

01:43.400 --> 01:44.600
It would return.

01:45.170 --> 01:47.870
So this is one way how a thread terminate.

01:47.900 --> 01:54.630
The second way is that that the child thread during its execution invokes an API thread underscore exit.

01:54.650 --> 02:04.310
So here if I simply type out that if a is equal equal five, then I want to terminate my thread.

02:05.090 --> 02:09.590
So what I'm going to do is that simply invoke an API thread.

02:09.590 --> 02:10.460
Exit zero.

02:10.920 --> 02:16.830
So this is one way in which my child thread gets terminated using an API thread.

02:16.830 --> 02:18.110
Underscore exit.

02:18.120 --> 02:18.840
Right.

02:18.930 --> 02:25.320
Now what does that zero represent in the bracket that we will going to discuss in detail in the subsequent

02:25.320 --> 02:26.640
section of this course.

02:26.940 --> 02:32.760
So you can make these little changes in your Hello World program that you have written and try out.

02:33.720 --> 02:36.670
Coming to the third point, that is thread cancellation.

02:36.690 --> 02:42.990
So here thread cancellation simply means that one thread is canceled or terminated by other thread.

02:43.020 --> 02:50.070
So in this example you can say that the thread t one creates a thread t two at the fourth point f right.

02:51.170 --> 02:57.290
Now let us suppose that the thread T2 is doing its work and it is somewhere doing its work at point

02:57.320 --> 02:57.890
B.

02:58.190 --> 03:00.200
Point B is some instruction.

03:00.290 --> 03:03.800
Point B is some instruction in the execution flow of the thread.

03:03.830 --> 03:05.210
T2 right.

03:05.240 --> 03:10.100
Now thread t1 wishes to terminate the thread t2 right.

03:10.100 --> 03:18.020
So thread t1 at any point of time can send the cancel request to the thread t2 right.

03:18.050 --> 03:25.130
When this cancel request is received by the thread t2 it don't matter what thread t2 was busy with,

03:25.160 --> 03:27.770
it will get terminated right.

03:28.280 --> 03:34.550
Now note that it is not necessary that it is only the parent thread which can cancel child thread.

03:34.580 --> 03:37.970
Even child thread can also cancel the parent thread.

03:38.570 --> 03:39.290
Right.

03:39.470 --> 03:42.230
So any thread can cancel any other thread.

03:42.260 --> 03:45.800
In fact, one thread can cancel many other threads.

03:45.800 --> 03:46.430
Right.

03:46.430 --> 03:47.660
So it all depends.

03:47.660 --> 03:51.500
Sending this cancellation request to the thread.

03:51.500 --> 03:55.400
A thread which receives this cancellation request will get cancelled.

03:55.610 --> 04:00.590
So we will discuss in detail regarding thread cancellation in the future section of this course.

04:00.620 --> 04:06.320
For now I have just given an overview that cancellation of the thread is the third way in which a thread

04:06.320 --> 04:08.330
can be cancelled or terminated.
