POSIX线程
#include<pthread.h>
int pthread_create(pthread_t? *thread,pthread_attr_t *attr
,void*(*start_routine)(void*),void*arg)
#include#include#include#include#include
void *thread_function(void *arg);
char message[]= "hello pthread" ;
int main(){???
int res ;???
pthread_t a_thread ;??
? void *thread_reasult;???
????? res = pthread_create(&a_thread,NULL,thread_funciton,(void*)message) ;???
if(res !=0){??
??? perror("thread creation? failed .");???
?? exit(EXIT_FAILURE);???
}?????
?? printf("waiting? for thread to finish ...");??
? res = phread_join(a_thread,&thread_result);??
? if(res !=0){???
?? perror("thread join? failed .");????
? exit(EXIT_FAILURE);???
}??
? printf("thread join , it retured %s ",(char*)thread_result);??
? printf("message is now? %s",message);?
?? exit(EXIT_SUCCESS) ;
}
void *thread_function(void *arg){
? printf("thread_function? is running? . argument was %s \n", (char*)arg);
? sleep(3) ;?
strcpy(message,"byte . ");?
pthread_exit("thank you for the cpu time . ");
}