zoukankan      html  css  js  c++  java
  • Linux pthread_exit()传递线程返回码

    (1)通过全局变量进行传递

    struct food
    {
        int a;
        int b;
        int c;
    };
    struct food apple;
    
    void* task1(void* arg)
    {
        apple.a = 27;
        apple.b = 12;
        apple.c = 39;
        pthread_exit((void*)&apple);
    }
    
    int main(int argc, char *argv[])
    {
        pthread_t thrd1, thrd2, thrd3;
        void* tret;
    pthread_create(&thrd1, NULL, (void*)task1, NULL); pthread_join(thrd1, (void*)&tret);    printf("The food:%d %d %d\n", ((struct food*)(tret))->a, ((struct food*)(tret))->b, ((struct food*)(tret))->c); printf("Main thread exit...\n"); return 0; }

     程序输出:

    [root@robot ~]# gcc thread_exit.c -lpthread -o thread_exit
    [root@robot ~]# ./thread_exit
    The food:27 12 39
    Main thread exit...
    [root@robot ~]#

    (2)通过malloc分配变量进行传递

    struct food
    {
        int a;
        int b;
        int c;
    };
    
    void* task1(void* arg)
    {
        struct food *apple = malloc(sizeof(struct food));
        apple->a = 23;
        apple->b = 82;
        apple->c = 59;
        pthread_exit((void*)apple);
    }
    
    int main(int argc, char *argv[])
    {
        pthread_t thrd1, thrd2, thrd3;
        void* tret;
    
        pthread_create(&thrd1, NULL, (void*)task1, NULL);
        pthread_join(thrd1, (void*)&tret);
    
        printf("The food:%d %d %d\n", ((struct food*)(tret))->a, ((struct food*)(tret))->b, ((struct food*)(tret))->c);
        free(((struct food*)tret));
        printf("Main thread exit...\n");
    
        return 0;
    }

     程序输出:

    [root@robot ~]# gcc thread_exit.c -lpthread -o thread_exit
    [root@robot ~]# ./thread_exit
    The food:23 82 59
    Main thread exit...
    [root@robot ~]#
  • 相关阅读:
    The Stanford Natural Language Processing Group
    HTTP和Socket
    数据挖掘之R与SQL
    redis运维的一些知识点
    ICTCLAS2011免费发布
    异步IO一定更好吗?
    试用wincvs2.0
    本地访问服务器上的wamp
    如何解决wamp中apache外部IP访问问题
    高级设置电脑系统windows7防火墙出错代码0×6D9原因与解决技巧
  • 原文地址:https://www.cnblogs.com/Robotke1/p/3053518.html
Copyright © 2011-2022 走看看