pthread_join

需要注意:所使用的存儲返回信息的結構,在調用者完成調用以後,必須仍然有效。

實例程序:

#include "apue.h"
#include <pthread.h>




void * thread1_fun(void *arg)
{
        printf("Thread 1 is runing\n");
        return ((void *) 1);
}


void * thread2_fun(void *arg)
{
        printf("Thread 2 is runing\n");
        pthread_exit((void *) 2);
}


int main()
{
        pthread_t td1,td2;
        int err;
        void *ret_ptr;


        err = pthread_create(&td1,NULL,thread1_fun,NULL);
        if (err != 0){
                err_sys("thread1 create failed");
        }


        err = pthread_create(&td2,NULL,thread2_fun,NULL);
        if (err != 0)
                err_sys("Thread2 create failed");




        err = pthread_join(td1,&ret_ptr);
        if (err != 0)
                err_exit(err,"get td1 return value error");
        printf("Thread1 return value %ld\n",ret_ptr);


        err = pthread_join(td2,&ret_ptr);
        if (err != 0)
                err_exit(err,"get td2 return value error");
        printf("Thread2 return value %ld\n",ret_ptr);


        exit(0);


}
~
~
~
~
~
~
~
~

編譯及運行結果:

gcc -Wall -ggdb3 11_3.c error.c -o get_ret_value -lpthread

./get_ret_value
Thread 1 is runing
Thread 2 is runing
Thread1 return value 1
Thread2 return value 2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章