Now we will see an example of how to detach a thread.
In this example we have included the required header files and.
This program is similar to one of the previous program showed.
But in this case we are using the concept of thread detach.
So the main function creates a thread and the id is specified in t1 underscore id and the thread starts
to execute from the function specified here.
Thread func and the thread func is defined in this case, and we are passing null as the arguments.
And if we note here the main thread is not calling any thread join.
Also, if we see in this definition of the thread function func.
Here we are calling p thread detach thread id of this thread.
So when we call p thread detach, this thread that is created detaches itself and it runs as an independent
thread.
And now there is no necessity for this thread to return its value to the main thread.
Also the thread when when the created thread terminates, the memory cleanup will be done automatically.
But in case of a joinable thread, if the thread terminates and if the main thread has not called a
p thread join, then a zombie thread is created.
Let's compile this and see the output.
So now we have compiled the program which is the program name and using LP thread.
We have linked the program using a p thread and we have created the output file.
Now let's run this file.
If you see here the main thread has already exited and now the thread function has started and then
it exits after executing all these instructions.
So we can note that this thread that is created is not returning any value, and the main thread does
not wait until the created thread terminates.
So this is the advantage of P3.
Detach.
Now let's see the next example.
Now let's see the concept of canceling a thread.
In this case we have a main thread here.
And this is the thread that we have created by calling Pthreadcreate.
So we have created a thread by calling Pthreadcreate.
And now we have specified the function from where the thread has to start executing.
And in the main thread we have, we are printing a for loop for a count of ten and sleeping for two
seconds, and it after every two seconds.
It prints the count value in the main thread, and after a two seconds into ten, that is, after 20s.
The main thread calls a cancel of t1 underscore id that is nothing but t1.
Underscore id specifies this thread, so this main thread is calling a cancel of this thread that is
created.
Now when this thread that is created is executing.
When the main thread calls per thread cancel based on the checkpoints, the thread running here terminates.
Let's compile this and see the output.
Now let's run this program.
Now if you see here we get two prints basically from main thread and the thread function that is created.
So for every two seconds the main thread is printing its count value.
Also the thread function is also executing similar loop here.
Let's decrease the font here.
Okay.
So if you see here after a count of nine the main thread sends a thread cancel and the thread that is
created gets terminated after sending a thread cancel.
Let's run the program once again.
We can observe that the thread create is created, is also executing, and the main thread is also executing.
So after the count of nine, the main thread sends a thread cancel to the created thread, and the thread
in the thread that is created terminates.
So if you see here after a count of nine, the main thread is sending a thread cancel and it is sending
a cancel for this id and that the thread that is that was running here gets terminated.
So this kind of uh uh use case can be seen in case let's say the main thread has created many threads
and due to some fatal error of one thread, all other thread needs to be terminated.
So in this case we can use the thread cancel by calling thread cancel from the main thread.
Also, there are some other use cases like uh uh basically like uh when we are developing some GUI based
application and if we want to call uh, thread, like we need to terminate some thread based on some
user action, then.