Hello and welcome!
In this video session we will see some different features of Pthread.
To begin with, let's start with Pthread join.
Let's see the syntax of Pthread.
Join here.
The Pthread join takes two parameters the Pthread underscore t data type and a double pointer to void
and it returns an integer.
So on success.
The Pthread join returns a zero, or it returns a positive number on error.
When a Pthread join is called from the main function or the main thread, it waits until the thread
specified here terminates, and the thread can also pass on the return value to the main thread.
The pthread join acts as a blocking call in the main thread, and it blocks until the thread specified
here returns or exits.
Also, we can compare the pthreadjoin with the wait PID the parent process.
When creates a child process, it waits for the child process to exit by calling wait PID.
In the same way, the main thread can call pthreadjoin in order to wait for the thread it has created
until it exits.
Let's see the example to understand better.
Let's.
In this example we include the required header files and here the complete program.
When it runs, it runs as a single process.
And it has a main thread here.
And the main thread has created a thread by using p thread underscore create.
And the created thread id is stored here in this variable t1 underscore id which is of type p thread
underscore t, and this is the function pointer which starts to execute when the thread is being scheduled.
So if we see here, let's uh.
Okay, so now in this the main thread is calling a p thread join and it is specifying the ID for which
it needs to join the thread.
Also the it passes the address of Arias.
So Arias is a void star.
And now passing the address of Arias is nothing, but it is passing in void star.
Star.
This is a double pointer to void.
And now when the thread starts executing from here, the main thread waits until this thread terminates
or returns a value.
So in this case we have taken a max of 100 and a count of zero, and in a for loop we increment the
count and then later, if the count is less than 500, we are passing this string, or we are returning
this string to this variable.
Else we are returning count greater than 500 to this variable.
So let's compile this and see the output.
So we have compiled the program and we have created the executable file here.
And as a note we have to specify the option minus LP thread in order to link with the Pthread library.
Now let's run the program.
Now if you see the thread func has started and now the the the thread that is created is exiting and
it is exit.
When it exits, it has sent the return value count less than 500.
So.
this is the string that this thread has returned to the main thread, and the main thread was waiting
until the thread specified here was terminated.
So this is the value of RHS is printed here by typecasting it to a car star.
So so we can see this.
The main thread and thread returned count less than 500.
So this is the parameter or the return value from the thread.
And later we can see that the main thread is exiting now.
So.
So the main thread exits only after the thread that it has specified in this p thread join has exited.
So now let's see what is the behavior if we do not include this p thread join.
So if you say hash of zero and index, that means this part of the code is not considered as Compilable,
so this code will not be compiled.
Now let's see the.
Let's compile once again.
Now let's run the program.
So in this case if you see the main thread has already exited.
Now later the thread func is exit exiting.
Now this means if we do not include this p thread join.
The main thread will not wait for any of its thread and it will terminate.
So in order that the main thread has to wait for the thread that it has created and collect the return
value, we need to call the p thread join.
So p thread join is a way of waiting for the thread that it has created, and receive the return value
from the thread.