1 #include <stdio.h> 2 #include <pthread.h> 3 #include <stdlib.h> 4 #include <errno.h> 5 6 void *pthread_fun(void *arg) 7 { 8 int b; 9 b = *(int *)arg; 10 printf("b = %d \n",b); 11 int i = 5 ; 12 while(i > 0) 13 { 14 printf("pthread start \n"); 15 sleep(1); 16 i -- ; 17 } 18 } 19 int main() 20 { 21 pthread_t pthread; 22 int a =10; 23 #if 0 24 if (pthread_create(&pthread,NULL,pthread_fun,NULL) < 0) 25 { 26 perror("fail to pthread_create"); 27 exit(1); 28 } 29 #endif 30 #if 1 31 if (pthread_create(&pthread,NULL,pthread_fun,&a) < 0) 32 { 33 perror("fail to pthread_create"); 34 exit(1); 35 } 36 #endif 37 printf("pthread create success\n"); 38 pthread_join(pthread,NULL);//等待线程的退出 39 printf("pthread exit \n"); 40 return 0; 41 }
时间: 2024-10-16 08:23:05