pthread_exit用法模板
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#define MAX_NUM 10
/* get student height and weight */
void *thread_get_student_info(void* arg)
{
int student_id = 0;
int *result = NULL;
student_id = *(int*)arg;
result = (int*)malloc(sizeof(int)*3);
memset(result,0,sizeof(int)*3);
result[0] = student_id;
/* the mothod of getting height and weight */
result[1] = result[0]*10 + 1;
result[2] = result[0]*10 + 2;
pthread_exit((void*)result);
}
int main()
{
int i = 0;
int err[MAX_NUM] = {0};
int *result[MAX_NUM] = {0};
pthread_t thread[MAX_NUM] = {0};
/* [0]:student_id [1]:height [2]:weight */
int student_list[MAX_NUM][3] = {{0}};
for(i = 0;i < MAX_NUM;i++)
{
student_list[i][0] = i;
err[i] = pthread_create(&thread[i],NULL,thread_get_student_info,&student_list[i][0]);
if(err[i])
{
printf("pthread_create error,i:%d\n",i);
}
}
for(i = 0;i < MAX_NUM;i++)
{
if(thread[i] == 0)
continue;
pthread_join(thread[i],(void*)&result[i]);
}
for(i = 0;i < MAX_NUM;i++)
{
student_list[i][1] = result[i][1];
student_list[i][2] = result[i][2];
printf("student:%d height:%d weight:%d\n",i,student_list[i][1],student_list[i][2]);
free(result[i]);
result[i] = NULL;
}
return 0;
}