WEBVTT

00:05.190 --> 00:10.980
So the third function callback is nothing, but it is a function which the new thread will going to

00:10.980 --> 00:11.960
execute.

00:11.970 --> 00:18.420
Write Such type of functions have a fixed prototype and the prototype should be should be that that

00:18.420 --> 00:20.490
these function must return void star.

00:20.490 --> 00:24.360
And the argument to this function is also void star, right?

00:24.360 --> 00:30.030
So you can see that the prototype of thread function callbacks is very generic, right?

00:30.360 --> 00:35.640
So as soon as the child thread starts, the child thread invokes this function, right?

00:35.640 --> 00:41.010
So in other words, this function will be executed in the context of a child thread, right?

00:41.550 --> 00:44.600
Now our new thread must do some useful work.

00:44.610 --> 00:48.130
Or maybe not useful, but a new thread must do some work.

00:48.150 --> 00:49.590
So what are we going to do?

00:49.590 --> 00:54.910
Is that I will simply extract the argument which was passed to our new thread.

00:54.930 --> 00:56.970
So what was the argument which was passed?

00:56.970 --> 01:01.050
The argument was nothing but the address of this input string.

01:01.050 --> 01:01.680
Right?

01:01.680 --> 01:07.830
So whatever memory you passed as the last argument to the P thread create function, the address of

01:07.860 --> 01:12.060
that memory will be available as an argument to this thread function callback.

01:12.060 --> 01:12.690
Right.

01:12.690 --> 01:15.540
So you simply type cast it into the car star.

01:15.540 --> 01:16.260
Right.

01:17.550 --> 01:23.910
And now what I will going to do is that I will going to create an infinite loop and inside that loop

01:23.910 --> 01:26.700
I will going to just print this string, right?

01:26.700 --> 01:33.060
And I will going to use sleep one so that our new thread take a rest of one second.

01:33.090 --> 01:38.370
Now in this Hello World program though, it is very simple and basic, but we will going to but we will

01:38.370 --> 01:41.680
going to discuss several basic points regarding Multithreading.

01:41.680 --> 01:45.100
Okay, so now let me compile this Hello World program.

01:45.100 --> 01:49.810
The instruction to compile a multi multithreaded program is written at the top of the file.

01:49.960 --> 01:55.720
So what you are going to do is that simply you can copy paste these lines, right?

01:56.470 --> 02:03.690
And note that for a multithreaded program you need to link your executable with the P thread library.

02:03.700 --> 02:08.800
So to specify the P thread library, you can simply write minus P thread.

02:08.800 --> 02:09.580
Right?

02:09.580 --> 02:17.350
And if you just press enter the hello world executable will be created and the expectation is that that

02:17.350 --> 02:25.870
the main thread should get paused right here and the child thread must run in this infinite loop.

02:25.870 --> 02:32.080
And every time the child thread must print this input string so you can see the same thing is happening

02:32.080 --> 02:32.770
right?

02:35.680 --> 02:41.530
So the child thread is running and it is printing the input string after every one second on the screen.

02:41.530 --> 02:47.920
In other words, the child thread is doing its job, whereas the main thread is halt at the line number

02:47.920 --> 02:48.960
77.

02:48.970 --> 02:49.690
Right.

02:51.940 --> 03:00.250
If we remove this line number 77 and if you execute this program again after compiling, of course,

03:01.180 --> 03:05.170
then you will see that nothing happens, right?

03:05.260 --> 03:12.820
Even before your child thread took a birth and start its life, your main thread had terminated and

03:12.820 --> 03:17.920
if your main thread terminates, then all the child threads in the program gets terminated automatically.

03:17.920 --> 03:18.580
Right?

03:19.450 --> 03:25.390
So in other words, by removing this pause call from the function, we have hardly given any chance

03:25.390 --> 03:29.080
to the child thread to take a birth and live its life.

03:29.350 --> 03:35.260
I will show you shortly in this section that how you can allow your main thread to terminate while having

03:35.360 --> 03:37.370
your child thread alive.

03:37.490 --> 03:44.030
So now we will go into analyze this Hello World Multithreaded program from various aspects and understand

03:44.030 --> 03:48.380
certain points regarding multithreading in the next couple of lecture videos.
