zoukankan      html  css  js  c++  java
  • ARTS-S C语言主线程获取子线程返回值

    #include<stdio.h>
    #include<stdlib.h>
    #include <pthread.h>
    
    typedef struct {
        int stu_num;
        char* stu_name;
    }Student;
    
    void *thr_fn1(void *arg) {
        Student* student = (Student*)malloc(sizeof(Student));
        student->stu_num = 1;
        student->stu_name = "name1";
        return((void *)student);
    }
    
    void *thr_fn2(void *arg) {printf("thread 2 exiting
    ");
        Student* student = (Student*)malloc(sizeof(Student));
        student->stu_num = 2;
        student->stu_name = "name2";
        return((void *)student);
    }
    
    int main(void) {
        int         err;
        pthread_t   tid1, tid2;
        void        *tret;
        err = pthread_create(&tid1, NULL, thr_fn1, NULL);
        if (err != 0)
            printf("can’t create thread 1:%d", err);
        err = pthread_create(&tid2, NULL, thr_fn2, NULL);
        if (err != 0)
            printf("can’t create thread 2:%d", err);
        err = pthread_join(tid1, &tret);
        if (err != 0)
            printf("can’t join with thread 1:%d", err);
    
        printf("thread 1 number=%d,name=%s
    ", ((Student*)tret)->stu_num, ((Student*)tret)->stu_name);
        err = pthread_join(tid2, &tret);
        if (err != 0)
            printf("can’t join with thread 2:%d", err);
        printf("thread 2 number=%d,name=%s
    ", ((Student*)tret)->stu_num, ((Student*)tret)->stu_name);
        exit(0);
    }
    // gcc -Wall main1.c -lpthread -o demo
    

    可用于主线程等子线程完成.

  • 相关阅读:
    《架构师》反思:系统可靠性
    表现力(转)
    4月反思
    30天敏捷结果(10) 强化你的一周
    认真对待学习(2月反思)
    Sort By Double
    12月反思 组内设计评审会议
    WPF框架的内存泄漏BUG
    OpenExpressApp 框架结构(2)
    绑定子类的泛型基类,反模式?
  • 原文地址:https://www.cnblogs.com/zhouyang209117/p/11144268.html
Copyright © 2011-2022 走看看