#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <pthread.h> /* * */ static int run = 1; static int retvalue ; void *start_routine(void *arg){ int *running =arg; printf("child thread is begining, the parameter is %s\n",*running); while(*running){ printf("child thread is rnning\n"); usleep(1); } printf("exitting..\n"); retvalue = 8; pthread_exit((void*) &retvalue); } int main(void) { pthread_t pt; int ret = -1; int times = 10; int i = 0; int *ret_join = NULL; ret = pthread_create(&pt,NULL,(void*)start_routine,&run); if(ret != 0){ printf("failed to create phread\n"); return 1; } //sleep(3); for(i=0;i<times;i++){ printf("this is main thread\n"); //sleep(3); } run = 0; pthread_join(pt,(void*)&ret_join); printf("the return value of thread: %d\n",*ret_join); return 0; }
运行结果
this is main thread this is main thread child thread is begining, the parameter is (null) exitting.. the return value of thread: 8