WEBVTT

00:00.440 --> 00:06.200
So guys, now let us discuss the example program that we will going to discuss in order to understand

00:06.200 --> 00:07.440
thread cancellation.

00:07.460 --> 00:14.000
So the program file is present in this path that is Multithreading Bible slash thread basics slash thread

00:14.000 --> 00:18.110
cancellation and the name of the file is master underscore slave one dot c.

00:18.290 --> 00:18.970
Right.

00:18.980 --> 00:25.280
So I have implemented a very basic multithreaded program and the program is all about to create five

00:25.310 --> 00:27.770
threads and all the five threads.

00:27.770 --> 00:32.540
Write a string into a file thread x dot txt where x is a thread id.

00:32.570 --> 00:33.320
Right.

00:33.440 --> 00:36.710
So just assign some thread id to each of these five threads.

00:36.710 --> 00:38.690
Let's say 012, three four.

00:38.780 --> 00:39.530
Right.

00:39.530 --> 00:47.180
And then each of these threads create a file thread x dot txt where x is the thread id and then each

00:47.180 --> 00:49.790
of these threads write a very simple string.

00:49.790 --> 00:53.420
I am thread and thread id into their respective files.

00:53.420 --> 00:54.110
Right.

00:54.110 --> 00:59.360
So you can see that all the five threads write a string into their respective text file.

00:59.360 --> 01:02.460
And this is what this program does, right?

01:02.670 --> 01:09.720
So I'm in the file master slave one dot c and you can go through this file implementation.

01:09.720 --> 01:16.590
It is very simple and you can simply compile this program using shell script, which I have written,

01:16.590 --> 01:25.140
and it will create an executable master slave one dot x simply execute and then simply execute this

01:25.140 --> 01:26.100
executable.

01:26.100 --> 01:26.850
Right?

01:27.730 --> 01:32.500
Now, when you run this executable, it will show you only one choice to select, which is cancel the

01:32.500 --> 01:33.220
thread.

01:33.250 --> 01:34.090
Right.

01:34.210 --> 01:41.440
So you can see that in the main program I have implemented a main menu kind of approach and the choice

01:41.440 --> 01:44.170
number one says that cancel the thread.

01:44.170 --> 01:50.350
Right now, the thread which needs to be canceled is identified by taking an input from the user.

01:50.380 --> 01:53.980
That input is nothing, but it is a thread ID, right.

01:54.160 --> 01:57.070
So you can see that as soon as the program starts.

01:57.070 --> 01:59.650
I am just creating five threads here.

01:59.650 --> 02:03.340
This macro and slaves is equated to five.

02:03.340 --> 02:03.970
Right.

02:04.000 --> 02:07.740
You can change this number to any value as per your choice.

02:07.750 --> 02:14.170
So here you can see that I am creating a loop and creating five threads, passing the respective thread

02:14.200 --> 02:15.550
ID as an argument.

02:15.550 --> 02:16.300
Right.

02:16.600 --> 02:22.690
So if you go into this function right into file, you will find that each of the thread is iterating

02:22.690 --> 02:25.660
into an infinite loop with a sleep value one.

02:25.660 --> 02:26.410
Right.

02:26.410 --> 02:32.110
And each thread is simply writing a text string into its respective text file.

02:32.120 --> 02:35.960
The name of the text file can be derived using as print function.

02:35.960 --> 02:36.650
Right.

02:37.700 --> 02:43.700
Using a sprinter function, you can cook up the file name using thread ID, and then you can open the

02:43.700 --> 02:45.680
file with that name in the right mode.

02:45.680 --> 02:46.400
Right.

02:46.400 --> 02:53.030
And then your thread can enter into an infinite loop writing a text string into its respective text

02:53.030 --> 02:53.540
file.

02:53.540 --> 02:54.260
Right.

02:55.070 --> 02:59.860
So now in the another window, you can see that I had already executed the executable.

02:59.870 --> 03:06.130
It simply means that each of the five threads must be writing a string into their files.

03:06.140 --> 03:11.690
So if you look into this directory, you can see that corresponding to each of the five threads, their

03:11.690 --> 03:13.910
respective text files have been created.

03:13.910 --> 03:14.690
Right?

03:14.780 --> 03:22.520
So using tail minus F command, you can check any of these file and see whether the thread is writing

03:22.520 --> 03:24.470
the content into this file or not.

03:24.470 --> 03:25.250
Right.

03:25.250 --> 03:31.370
So you can check any of these text files and you will find that each of the thread is writing this string

03:31.370 --> 03:32.540
into their file.

03:33.140 --> 03:39.320
So I expect you that you go through this very short program which is only 90 lines of code.

03:39.350 --> 03:40.820
Understand this program.

03:40.820 --> 03:46.010
And then in the next lecture video we will see how we can implement thread cancellation.

03:46.010 --> 03:46.730
Right?

03:47.150 --> 03:50.300
Basically, we need to implement this case, number one.

03:50.330 --> 03:54.320
That is, we need to cancel the thread based on the user input here.

03:54.320 --> 03:58.650
The user input is nothing, but it is the thread ID, right.

03:59.610 --> 04:05.130
So if the user provides an input, say three, we will going to cancel the thread with thread ID three.

04:05.160 --> 04:09.490
Here, thread id simply will be the index of this array.

04:09.510 --> 04:14.250
This is the array which I have taken in order to represent the five threads writing the content into

04:14.250 --> 04:15.130
a text file.

04:15.150 --> 04:15.900
Right.

04:15.930 --> 04:21.680
This is the array of thread handles and at the time of creation of the thread it is.

04:21.690 --> 04:27.000
I obtained the thread handle from that array in order to pass as a first argument to this thread.

04:27.030 --> 04:28.380
Underscore create API.

04:28.590 --> 04:29.340
Right.

04:30.590 --> 04:36.440
So basically in this program you have all the thread handles stored in this global array so that you

04:36.440 --> 04:42.590
have access to all the thread handles and we will see that how we can make use of this global array

04:42.710 --> 04:49.930
storing thread handles in order to cancel the threads whose ID has been entered or asked from the user.

04:49.940 --> 04:50.660
Right.
