pthread_exit用法模板

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值