1#include <pthread.h>2#include <unistd.h>3#include <stdio.h>4#include <stdlib.h>5#include <string.h>6#include <error.h>7 8 void*THR_FN1 (void*Arg)9 {Tenprintf"Thread 1 returning\n"); One return((void*)1); A } - - the void*THR_FN2 (void*Arg) - { -printf"Thread 2 exiting\n"); -Pthread_exit ((void*)2); + } - + intMainvoid) A { at interr; - pthread_t Tid1,tid2; - void*Tret; - -Err=pthread_create (&tid1,null,thr_fn1,null);//create thread 1 - if(err!=0) infprintf (stderr,"errno:%s", Strerror (err));//Create Thread 2 -Err=pthread_create (&tid2,null,thr_fn2,null); to if(err!=0) +fprintf (stderr,"errno:%s", Strerror (err)); -Err=pthread_join (tid1,&Tret);//return value of Receive thread 1 the if(err!=0) *fprintf (stderr,"errno:%s", Strerror (err)); $printf"thread 1 exit code%d\n",(int) tret);Panax Notoginseng -Err=pthread_join (tid2,&Tret);//return value of Receive thread 2 the if(err!=0) +fprintf (stderr,"errno:%s", Strerror (err)); Aprintf"thread 2 exit code%d\n",(int) tret); theExit0); +}
Program Compile and run:
1 gcc-o pthread pthread.c-lpthread22 exiting31 Returning411522
Pthread_exit&&pthread_join function