Hello and welcome!
In this video session, we will see the concepts of detaching a thread and cancelling a thread.
In the previous video session, we saw the P thread join in the P thread.
Join the created thread can terminate and send the status to the main thread.
When the main thread calls a function call p thread join.
Now if a joinable thread terminates and if the main thread does not call a p thread join, then a situation
similar to a zombie thread is created and a zombie thread consumes the memory resource of the system.
So whenever we use joinable threads, we have to make sure that the main thread calls the p thread join
function.
Similarly, we have one more kind of thread called as p thread detach.
In this, the thread that is created can be detached.
This means the there is no need for the main thread to call p thread, join and collect the exit status
or return value from the created thread.
Also, in case of a detachable thread, the memory cleanup is done automatically and the p thread the
detached.
The detached thread will not send any return status to the main thread.
The p thread detach syntax is as follows.
We need to include the p, h and p thread.
Underscore.
Detach is the function call and it takes the parameter p thread underscore t, and we have to specify
the thread id that needs to be detached.
So this can be called from the main thread or from the thread that is created.
So in the in case we are calling this function from the thread that is created, then here we need to
specify p thread underscore self or we should specify the self thread id in order to detach the thread.
Upon success it returns a zero or a positive number on error.
In the example we will see how to use a detach.
Now let us see what is a p thread.
Cancel.
So let us see the syntax first.
So p thread cancel.
Also we need to include p dot h p thread cancel.
This is the function call and we need to specify for which thread we want to cancel.
So in this case this is nothing.
But it's a kind of terminating the thread specified here.
So let's say the main thread that has created a the other thread.
So the main thread can call p thread cancel and specify the thread ID of the created thread.
So once calling this the the thread that is specified in this parameter will be terminated.
So this is called as canceling the thread.
Now let us see the example of both p thread cancel and p thread detach.