#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void * function(void *);
main()
{
pthread_t p[5];
int j;
int *arg1[4];
int arr[5]={1,2,3,4,5};
for(j=0;j<=4;j++)
pthread_create(&p[j],NULL,function,&arr[j]);
for(j=0;j<=4;j++)
pthread_join(p[j],(void **)&arg1[j]);
for(j=0;j<=4;j++)
printf("\n%d",*arg1[j]);
}
void * function(void *arg)
{
int *i = (int *) arg;
pthread_exit(i);
}
Output:
-1498551536
32767
3
4
5
Q.1) It is always printing junk values for first two values. Why is it so? please correct me if anything is wrong here.
When i changed the code like below, it is properly printing 1,2,3,4,5.
for(j=0;j<=4;j++)
{
pthread_join(p[j],(void **)&arg1[j]);
printf("\n%d",*arg1[j]);
}
Q.2) what are the different methods of returning values from thread? Can you please summarize all the methods with example and explain which one is the method to follow?