#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) 항상 처음 두 값에 대한 정크 값을 인쇄합니다. 왜 그래야만하지? 여기에 뭐가 잘못 되었다면 저를 바로 잡으십시오.Pthread_join & Pthread_exit in c
아래 코드를 변경하면 올바르게 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) 스레드로부터 값을 반환하는 다른 방법은 무엇입니까? 모든 방법을 예를 들어 요약하고 따르는 방법을 설명해 주시겠습니까?