So welcome back, guys.
Now, in this section of the course, we will going to understand what are joinable and detached threads,
what are their properties and how do they work.
So to start with a thread when created using p thread underscore create API, it can be created in one
of the two modes that is, either the thread could be joinable or the thread is detached.
Right.
We pass an argument to this p thread underscore create API which decides whether the new thread that
will going to be created is a joinable thread or detached thread.
We will see in the demo program how to specify that argument.
Now let us first try to understand what are joinable threads, what are their properties?
What are their benefits and how do they work?
So let us suppose that you have written a multithreaded program, and in that multithreaded program
you have one of the threads.
Let us call that thread as parent thread.
Right.
Now, when this parent thread was executing in its execution flow, let us say that at some point of
time the parent thread decides to create another thread, right?
So at point F, which is a fork point, the parent thread invokes an API thread, underscore, create
and create a new thread in joinable mode.
Right.
So let us suppose that this particular thread is a joinable thread.
So after the fork point f the parent thread continue along the course of its execution flow and new
joinable thread that is created will also start doing its work and continue to execute in its own execution
flow.
Right.
Now, let us suppose that while the Joinable child thread is busy doing its own work, the parent thread
invokes an API p thread underscore join.
Let us suppose that this API is invoked at this particular point, which is a point called J.
Let us suppose that the parent thread has invoked this p thread.
Underscore join API at some point in its execution flow and let us represent that point using the letter
J.
Right.
So this is actually a join point.
Now what happens as soon as the parent thread invokes an api p thread underscore join?
What is the functionality of this API?
The functionality of this API is that that the parent thread gets blocked here, right?
Right at the join point, the parent thread execution gets blocked.
And till what time it will stay blocked.
It will stay blocked until the new child thread come back and join it.
Right.
So it simply means that when this child thread finishes its execution, let us represent the point where
it finishes its execution by a letter T.
So t represents the termination point of the thread, and a child thread can choose any of the mode
to terminate that is either by explicitly calling p thread underscore exit API or it simply returns
from its thread function.
Right.
So whatever be the case when the child thread terminates.
It sends a joint signal to the parent thread.
Right?
So it simply means that the parent thread, which was blocked at this point, will resume its execution
further.
Right?
So note that resources of the new thread which has just terminated and join the parent thread are released
only when it joins the parent thread right until the child thread joins the parent thread.
The resources occupied by the child thread are not cleaned off the system and once the child thread
joins the parent thread, then parent thread resumes its execution beyond the join point.
So this is how Joinable thread works.
The Joinable thread after its termination have to join the parent thread which is blocked on the join
point.
If there is no parent thread which is blocked on the join point and the child thread which is a joinable
thread terminates, then the resources of the child thread would not be released.
Right.
So it would be wrong programming if you create a joinable thread and upon termination of the joinable
thread, you do not allow the child thread to join some other thread blocked at the join point so resources
of the joinable thread are not released until it joins the parent thread at the join point.
A joinable thread can be converted into detached thread while it is running or vice versa.
Right.
So at the runtime also that is when the Joinable child thread is busy doing its work.
It is also possible to change the nature of the child thread at the time when it is actually executing.
The Posix standard provides certain APIs which allows you to do so.
We will going to see how to use those APIs when we will be discussing demo program.
And by default, if you create a new thread and you do not specify the mode of that thread, then by
default the thread that is created is a joinable thread.
And Joinable thread may return the result to the parent thread.
Here join thread means the parent thread.
Right.
So this is also we will see in the demo program that how a child thread returns its result to the parent
thread at the time of joining.
So this was the concept of joinable threads.
We will we will do an assignment on joinable threads and we will see that how joinable threads can help
us to resolve certain type of multithreaded problems.
Now.
Next let us discuss test thread.