So guys, now let us discuss the example program that we will going to discuss in order to understand
thread cancellation.
So the program file is present in this path that is Multithreading Bible slash thread basics slash thread
cancellation and the name of the file is master underscore slave one dot c.
Right.
So I have implemented a very basic multithreaded program and the program is all about to create five
threads and all the five threads.
Write a string into a file thread x dot txt where x is a thread id.
Right.
So just assign some thread id to each of these five threads.
Let's say 012, three four.
Right.
And then each of these threads create a file thread x dot txt where x is the thread id and then each
of these threads write a very simple string.
I am thread and thread id into their respective files.
Right.
So you can see that all the five threads write a string into their respective text file.
And this is what this program does, right?
So I'm in the file master slave one dot c and you can go through this file implementation.
It is very simple and you can simply compile this program using shell script, which I have written,
and it will create an executable master slave one dot x simply execute and then simply execute this
executable.
Right?
Now, when you run this executable, it will show you only one choice to select, which is cancel the
thread.
Right.
So you can see that in the main program I have implemented a main menu kind of approach and the choice
number one says that cancel the thread.
Right now, the thread which needs to be canceled is identified by taking an input from the user.
That input is nothing, but it is a thread ID, right.
So you can see that as soon as the program starts.
I am just creating five threads here.
This macro and slaves is equated to five.
Right.
You can change this number to any value as per your choice.
So here you can see that I am creating a loop and creating five threads, passing the respective thread
ID as an argument.
Right.
So if you go into this function right into file, you will find that each of the thread is iterating
into an infinite loop with a sleep value one.
Right.
And each thread is simply writing a text string into its respective text file.
The name of the text file can be derived using as print function.
Right.
Using a sprinter function, you can cook up the file name using thread ID, and then you can open the
file with that name in the right mode.
Right.
And then your thread can enter into an infinite loop writing a text string into its respective text
file.
Right.
So now in the another window, you can see that I had already executed the executable.
It simply means that each of the five threads must be writing a string into their files.
So if you look into this directory, you can see that corresponding to each of the five threads, their
respective text files have been created.
Right?
So using tail minus F command, you can check any of these file and see whether the thread is writing
the content into this file or not.
Right.
So you can check any of these text files and you will find that each of the thread is writing this string
into their file.
So I expect you that you go through this very short program which is only 90 lines of code.
Understand this program.
And then in the next lecture video we will see how we can implement thread cancellation.
Right?
Basically, we need to implement this case, number one.
That is, we need to cancel the thread based on the user input here.
The user input is nothing, but it is the thread ID, right.
So if the user provides an input, say three, we will going to cancel the thread with thread ID three.
Here, thread id simply will be the index of this array.
This is the array which I have taken in order to represent the five threads writing the content into
a text file.
Right.
This is the array of thread handles and at the time of creation of the thread it is.
I obtained the thread handle from that array in order to pass as a first argument to this thread.
Underscore create API.
Right.
So basically in this program you have all the thread handles stored in this global array so that you
have access to all the thread handles and we will see that how we can make use of this global array
storing thread handles in order to cancel the threads whose ID has been entered or asked from the user.
Right.