Linux下多线程程序设计实验

 (1)线程创建pthread_create()

 

 
 
  1. #include <stdio.h> 
  2. #include <pthread.h> 
  3.  
  4. void *myThread1(void
  5.     int i; 
  6.     for (i=0; i<100; i++) 
  7.     { 
  8.         printf("This is the 1st pthread,created by zieckey.\n"); 
  9.         sleep(1);//Let this thread to sleep 1 second,and then continue to run 
  10.     } 
  11.  
  12. void *myThread2(void
  13.     int i; 
  14.     for (i=0; i<100; i++) 
  15.     { 
  16.         printf("This is the 2st pthread,created by zieckey.\n"); 
  17.         sleep(1); 
  18.     } 
  19.  
  20. int main() 
  21.     int i=0, ret=0; 
  22.     pthread_t id1,id2; 
  23.      
  24.     /*创建线程1*/ 
  25.     ret = pthread_create(&id1, NULL, (void*)myThread1, NULL); 
  26.     if (ret) 
  27.     { 
  28.         printf("Create pthread error!\n"); 
  29.         return 1; 
  30.     } 
  31.      
  32.     /*创建线程2*/ 
  33.     ret = pthread_create(&id2, NULL, (void*)myThread2, NULL); 
  34.     if (ret) 
  35.     { 
  36.         printf("Create pthread error!\n"); 
  37.         return 1; 
  38.     } 
  39.      
  40.     pthread_join(id1, NULL); 
  41.     pthread_join(id2, NULL); 
  42.      
  43.     return 0; 

(2)线程等待pthread_join()

 

 
 
  1. #include <pthread.h> 
  2. #include <unistd.h> 
  3. #include <stdio.h> 
  4.  
  5. void *thread(void *str) 
  6.     int i; 
  7.     for (i = 0; i < 10; ++i) 
  8.     { 
  9.         sleep(2); 
  10.         printf( "This in the thread : %d\n" , i ); 
  11.     } 
  12.     return NULL; 
  13.  
  14. int main() 
  15.     pthread_t pth; 
  16.     int i; 
  17.     int ret = pthread_create(&pth, NULL, thread, (void *)(i)); 
  18.      
  19.     pthread_join(pth, NULL); 
  20.     printf("123\n"); 
  21.     for (i = 0; i < 10; ++i) 
  22.     { 
  23.         //sleep(1); 
  24.         printf( "This in the main : %d\n" , i ); 
  25.     } 
  26.      
  27.     return 0; 

(3)线程清除pthread_cleanup_push(),pthread_cleanup_pop()

 

 

 
 
  1. #include <stdio.h> 
  2. #include <pthread.h> 
  3. #include <unistd.h> 
  4. void *clean(void *arg) 
  5.     printf("cleanup :%s  \n",(char *)arg); 
  6.     return (void *)0; 
  7. void *thr_fn1(void *arg) 
  8.     printf("thread 1 start  \n"); 
  9.     pthread_cleanup_push( (void*)clean,"thread 1 first handler"); 
  10.     pthread_cleanup_push( (void*)clean,"thread 1 second hadler"); 
  11.     printf("thread 1 push complete  \n"); 
  12.     if(arg) 
  13.     { 
  14.         return((void *)1); 
  15.     } 
  16.     pthread_cleanup_pop(0); 
  17.     pthread_cleanup_pop(0); 
  18.     return (void *)1; 
  19.  
  20.  
  21. void *thr_fn2(void *arg) 
  22.     printf("thread 2 start  \n"); 
  23.     pthread_cleanup_push( (void*)clean,"thread 2 first handler"); 
  24.     pthread_cleanup_push( (void*)clean,"thread 2 second handler"); 
  25.     printf("thread 2 push complete  \n"); 
  26.     if(arg) 
  27.     { 
  28.         pthread_exit((void *)2); 
  29.     } 
  30.     pthread_cleanup_pop(0); 
  31.     pthread_cleanup_pop(0); 
  32.     pthread_exit((void *)2); 
  33. }  
  34.  
  35. int main(void
  36.     int err; 
  37.     pthread_t tid1,tid2; 
  38.     void *tret; 
  39.  
  40.     err=pthread_create(&tid1,NULL,thr_fn1,(void *)1); 
  41.     if(err!=0) 
  42.     { 
  43.         printf("error .... \n"); 
  44.         return -1; 
  45.     } 
  46.     err=pthread_create(&tid2,NULL,thr_fn2,(void *)1); 
  47.  
  48.     if(err!=0) 
  49.     { 
  50.         printf("error .... \n"); 
  51.         return -1; 
  52.     } 
  53.     err=pthread_join(tid1,&tret); 
  54.     if(err!=0) 
  55.     { 
  56.         printf("error .... \n"); 
  57.         return -1; 
  58.     } 
  59.     printf("thread 1 exit code %d  \n",(int)tret); 
  60.  
  61.     err=pthread_join(tid2,&tret); 
  62.     if(err!=0) 
  63.     { 
  64.         printf("error .... "); 
  65.         return -1; 
  66.     } 
  67.  
  68.     printf("thread 2 exit code %d  \n",(int)tret); 
  69.      
  70.     return 1; 

 

(4)线程传参thread_int.c,thread_string.c,thread_struct.c

 

 
 
  1. #include <stdio.h> 
  2. #include <pthread.h> 
  3. #include <unistd.h> 
  4.  
  5. void *create(void *arg) 
  6.     int *num; 
  7.     num=(int *)arg; 
  8.     printf("create parameter is %d \n",*num); 
  9.     return (void *)0; 
  10.  
  11. int main(int argc ,char *argv[]) 
  12.     pthread_t tidp; 
  13.     int error; 
  14.      
  15.     int test=4; 
  16.     int *attr=&test; 
  17.      
  18.     error=pthread_create(&tidp,NULL,create,(void *)attr); 
  19.  
  20.     if(error) 
  21.         { 
  22.         printf("pthread_create is created is not created ... \n"); 
  23.         return -1; 
  24.         } 
  25.     sleep(1); 
  26.     printf("pthread_create is created ...\n"); 
  27.     return 0;         

 

 
 
  1. #include <pthread.h> 
  2. #include <stdio.h> 
  3. #include <unistd.h> 
  4.  
  5. void *create(void *arg) 
  6.     char *name; 
  7.     name=(char *)arg; 
  8.     printf("The parameter passed from main function is %s  \n",name); 
  9.     return (void *)0; 
  10.  
  11. int main(int argc, char *argv[]) 
  12.     char *a="zieckey"
  13.     int error; 
  14.     pthread_t tidp; 
  15.  
  16.     error=pthread_create(&tidp, NULL, create, (void *)a); 
  17.  
  18.     if(error!=0) 
  19.     { 
  20.         printf("pthread is not created.\n"); 
  21.         return -1; 
  22.     } 
  23.     sleep(1); 
  24.     printf("pthread is created... \n"); 
  25.     return 0; 
  26. }     

 

 
 
  1. #include <stdio.h> 
  2. #include <pthread.h> 
  3. #include <unistd.h> 
  4. #include <stdlib.h> 
  5.  
  6. struct menber 
  7.     int a; 
  8.     char *s; 
  9. }; 
  10.  
  11. void *create(void *arg) 
  12.     struct menber *temp; 
  13.     temp=(struct menber *)arg; 
  14.     printf("menber->a = %d  \n",temp->a); 
  15.     printf("menber->s = %s  \n",temp->s); 
  16.     return (void *)0; 
  17.  
  18. int main(int argc,char *argv[]) 
  19.     pthread_t tidp; 
  20.     int error; 
  21.     struct menber *b; 
  22.     b=(struct menber *)malloc( sizeof(struct menber) ); 
  23.     b->a = 4; 
  24.     b->s = "zieckey"
  25.  
  26.     error = pthread_create(&tidp, NULL, create, (void *)b); 
  27.  
  28.     if( error ) 
  29.     { 
  30.         printf("phread is not created...\n"); 
  31.         return -1; 
  32.     } 
  33.     sleep(1); 
  34.     printf("pthread is created...\n"); 
  35.     return 0; 

(5)进程和线程共享数据thread_share.c

 

 
 
  1. #include <stdio.h> 
  2. #include <pthread.h> 
  3. #include <unistd.h> 
  4.  
  5. //static int a=4; 
  6.  
  7. int a = 1; 
  8.  
  9. void *create(void *arg) 
  10.     printf("new pthread ... \n"); 
  11.     printf("a=%d  \n",a); 
  12.     return (void *)0; 
  13.  
  14. int main(int argc,char *argv[]) 
  15.     pthread_t tidp; 
  16.     int error; 
  17.      
  18.     int a=5; 
  19.  
  20.     printf("a = %d\n",a); 
  21.      
  22.     error=pthread_create(&tidp, NULL, create, NULL); 
  23.  
  24.     if(error!=0) 
  25.     { 
  26.         printf("new thread is not create ... \n"); 
  27.         return -1; 
  28.     } 
  29.      
  30.     sleep(1); 
  31.      
  32.     printf("new thread is created ... \n"); 
  33.     return 0; 

 

本文出自 “Mr~钟” 博客,请务必保留此出处http://6386296.blog.51cto.com/6376296/1121600

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值