WEBVTT

00:06.570 --> 00:08.220
So welcome back, guys.

00:08.250 --> 00:13.850
Now, in this section of the course, we will going to understand what are joinable and detached threads,

00:13.860 --> 00:17.130
what are their properties and how do they work.

00:18.360 --> 00:25.740
So to start with a thread when created using p thread underscore create API, it can be created in one

00:25.740 --> 00:31.710
of the two modes that is, either the thread could be joinable or the thread is detached.

00:31.710 --> 00:32.400
Right.

00:32.430 --> 00:39.210
We pass an argument to this p thread underscore create API which decides whether the new thread that

00:39.210 --> 00:42.900
will going to be created is a joinable thread or detached thread.

00:42.930 --> 00:46.980
We will see in the demo program how to specify that argument.

00:47.010 --> 00:51.750
Now let us first try to understand what are joinable threads, what are their properties?

00:51.750 --> 00:54.570
What are their benefits and how do they work?

00:56.300 --> 01:01.730
So let us suppose that you have written a multithreaded program, and in that multithreaded program

01:01.730 --> 01:03.500
you have one of the threads.

01:03.530 --> 01:06.420
Let us call that thread as parent thread.

01:06.440 --> 01:07.160
Right.

01:07.910 --> 01:14.900
Now, when this parent thread was executing in its execution flow, let us say that at some point of

01:14.900 --> 01:19.790
time the parent thread decides to create another thread, right?

01:19.820 --> 01:26.660
So at point F, which is a fork point, the parent thread invokes an API thread, underscore, create

01:26.660 --> 01:29.690
and create a new thread in joinable mode.

01:29.750 --> 01:30.500
Right.

01:30.530 --> 01:34.640
So let us suppose that this particular thread is a joinable thread.

01:35.180 --> 01:43.040
So after the fork point f the parent thread continue along the course of its execution flow and new

01:43.040 --> 01:50.270
joinable thread that is created will also start doing its work and continue to execute in its own execution

01:50.270 --> 01:50.750
flow.

01:50.750 --> 01:51.500
Right.

01:52.200 --> 01:59.730
Now, let us suppose that while the Joinable child thread is busy doing its own work, the parent thread

01:59.730 --> 02:02.910
invokes an API p thread underscore join.

02:03.330 --> 02:09.540
Let us suppose that this API is invoked at this particular point, which is a point called J.

02:10.140 --> 02:13.190
Let us suppose that the parent thread has invoked this p thread.

02:13.200 --> 02:20.520
Underscore join API at some point in its execution flow and let us represent that point using the letter

02:20.520 --> 02:21.040
J.

02:21.060 --> 02:21.770
Right.

02:21.780 --> 02:24.450
So this is actually a join point.

02:24.540 --> 02:29.670
Now what happens as soon as the parent thread invokes an api p thread underscore join?

02:29.700 --> 02:32.340
What is the functionality of this API?

02:32.460 --> 02:38.820
The functionality of this API is that that the parent thread gets blocked here, right?

02:39.030 --> 02:43.980
Right at the join point, the parent thread execution gets blocked.

02:45.260 --> 02:47.750
And till what time it will stay blocked.

02:47.780 --> 02:52.640
It will stay blocked until the new child thread come back and join it.

02:52.760 --> 02:53.600
Right.

02:53.780 --> 03:02.090
So it simply means that when this child thread finishes its execution, let us represent the point where

03:02.090 --> 03:04.790
it finishes its execution by a letter T.

03:04.820 --> 03:10.960
So t represents the termination point of the thread, and a child thread can choose any of the mode

03:10.970 --> 03:17.930
to terminate that is either by explicitly calling p thread underscore exit API or it simply returns

03:17.930 --> 03:19.490
from its thread function.

03:19.490 --> 03:20.300
Right.

03:20.900 --> 03:24.860
So whatever be the case when the child thread terminates.

03:26.060 --> 03:29.860
It sends a joint signal to the parent thread.

03:29.870 --> 03:30.650
Right?

03:31.340 --> 03:38.480
So it simply means that the parent thread, which was blocked at this point, will resume its execution

03:38.480 --> 03:39.130
further.

03:39.140 --> 03:39.890
Right?

03:41.090 --> 03:47.930
So note that resources of the new thread which has just terminated and join the parent thread are released

03:47.930 --> 03:53.820
only when it joins the parent thread right until the child thread joins the parent thread.

03:53.840 --> 04:01.370
The resources occupied by the child thread are not cleaned off the system and once the child thread

04:01.370 --> 04:08.300
joins the parent thread, then parent thread resumes its execution beyond the join point.

04:08.540 --> 04:11.260
So this is how Joinable thread works.

04:11.270 --> 04:18.170
The Joinable thread after its termination have to join the parent thread which is blocked on the join

04:18.170 --> 04:18.980
point.

04:19.160 --> 04:26.000
If there is no parent thread which is blocked on the join point and the child thread which is a joinable

04:26.000 --> 04:30.890
thread terminates, then the resources of the child thread would not be released.

04:30.920 --> 04:31.550
Right.

04:31.550 --> 04:37.730
So it would be wrong programming if you create a joinable thread and upon termination of the joinable

04:37.730 --> 04:47.220
thread, you do not allow the child thread to join some other thread blocked at the join point so resources

04:47.220 --> 04:52.200
of the joinable thread are not released until it joins the parent thread at the join point.

04:52.230 --> 04:57.300
A joinable thread can be converted into detached thread while it is running or vice versa.

04:57.300 --> 04:58.020
Right.

04:58.410 --> 05:05.190
So at the runtime also that is when the Joinable child thread is busy doing its work.

05:05.220 --> 05:12.090
It is also possible to change the nature of the child thread at the time when it is actually executing.

05:12.120 --> 05:16.950
The Posix standard provides certain APIs which allows you to do so.

05:17.130 --> 05:22.050
We will going to see how to use those APIs when we will be discussing demo program.

05:22.050 --> 05:28.620
And by default, if you create a new thread and you do not specify the mode of that thread, then by

05:28.620 --> 05:32.340
default the thread that is created is a joinable thread.

05:33.340 --> 05:37.720
And Joinable thread may return the result to the parent thread.

05:37.750 --> 05:40.060
Here join thread means the parent thread.

05:40.060 --> 05:40.720
Right.

05:41.390 --> 05:48.320
So this is also we will see in the demo program that how a child thread returns its result to the parent

05:48.320 --> 05:50.450
thread at the time of joining.

05:53.270 --> 05:55.940
So this was the concept of joinable threads.

05:55.940 --> 06:02.030
We will we will do an assignment on joinable threads and we will see that how joinable threads can help

06:02.030 --> 06:06.230
us to resolve certain type of multithreaded problems.

06:07.000 --> 06:07.480
Now.

06:07.480 --> 06:09.430
Next let us discuss test thread.
