Chinaunix首页 | 论坛 | 博客
  • 博客访问: 71734
  • 博文数量: 24
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 245
  • 用 户 组: 普通用户
  • 注册时间: 2014-12-10 08:06
文章分类
文章存档

2015年(24)

我的朋友

分类: LINUX

2015-06-02 10:14:36

编程实现三个线程ABC,并让它们顺次打印ABC

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<pthread.h>
  4. #include<semaphore.h>
  5. #include<string.h>

  6. /*
  7.  *声明3个线程
  8.  */
  9. void *thread1_func(void *arg);
  10. void *thread2_func(void *arg);
  11. void *thread3_func(void *arg);
  12. sem_t sem1 ;
  13. sem_t sem2 ;
  14. sem_t sem3 ;

  15. pthread_t thread1 = 0;
  16. pthread_t thread2 = 0;
  17. pthread_t thread3 = 0;

  18. int main(void)
  19. {
  20.     int res = 0;
  21.     
  22.     void *thread_return = (void *)0;
  23.     /*
  24.      *创建3个信号量
  25.      */
  26.     res = sem_init(&sem1, 0, 0);
  27.     if(res != 0)
  28.     {
  29.         perror("fail to create sem1\n");
  30.         exit(EXIT_FAILURE);
  31.     }
  32.     res = sem_init(&sem2, 0, 0);
  33.     if(res != 0)
  34.     {
  35.         perror("fail to create sem2\n");
  36.         exit(EXIT_FAILURE);
  37.     }
  38.     res = sem_init(&sem3, 0, 0);
  39.     if(res != 0)
  40.     {
  41.         perror("fail to create sem3\n");
  42.         exit(EXIT_FAILURE);
  43.     }
  44.     /*
  45.      *创建3个线程
  46.      */
  47.     res = pthread_create(&thread1, NULL, thread1_func, (void*)"A");
  48.     if(res != 0)
  49.     {
  50.         perror("fail to create thread1\n");
  51.         exit(EXIT_FAILURE);
  52.     }
  53.     res = pthread_create(&thread2, NULL, thread2_func, (void*)"B");
  54.     if(res != 0)
  55.     {
  56.         perror("fail to create thread2\n");
  57.         exit(EXIT_FAILURE);
  58.     }
  59.     res = pthread_create(&thread3, NULL, thread3_func, (void*)"C");
  60.     if(res != 0)
  61.     {
  62.         perror("fail to create thread3\n");
  63.         exit(EXIT_FAILURE);
  64.     }
  65.     
  66.     res = sem_post(&sem1);
  67.     if(res != 0)
  68.     {
  69.         perror("fail to post sem1\n");
  70.         exit(EXIT_FAILURE);
  71.     }
  72.     
  73.     /*
  74.      *等待线程执行
  75.      */
  76.     printf("wait for thread to finish\n");
  77.     res = pthread_join(thread1, &thread_return);
  78.     if(res != 0)
  79.     {
  80.         perror("fail to jion thread1\n");
  81.         exit(EXIT_FAILURE);
  82.     }
  83.     res = pthread_join(thread2, &thread_return);
  84.     if(res != 0)
  85.     {
  86.         perror("fail to jion thread2\n");
  87.         exit(EXIT_FAILURE);
  88.     }
  89.     res = pthread_join(thread3, &thread_return);
  90.     if(res != 0)
  91.     {
  92.         perror("fail to jion thread3\n");
  93.         exit(EXIT_FAILURE);
  94.     }
  95.     return 0;
  96. }

  97. void *thread1_func(void *arg)
  98. {
  99.     int reg = 0;
  100.     sem_wait(&sem1);
  101.     if(reg != 0)
  102.     {
  103.         perror("fail to wait sem1\n");
  104.         exit(EXIT_FAILURE);
  105.     }
  106.     printf("%s\n", (char *)arg);
  107.     flush(stdout);
  108.     sem_post(&sem2);
  109.     sem_destroy(&sem1);
  110.     pthread_exit(0);
  111. }


  112. void *thread2_func(void *arg)
  113. {
  114.     int reg = 0;
  115.     sem_wait(&sem2);
  116.     if(reg != 0)
  117.     {
  118.         perror("fail to wait sem2\n");
  119.         exit(EXIT_FAILURE);
  120.     }
  121.     printf("%s", (char*)arg);
  122.     flush(stdout);
  123.     sem_post(&sem3);
  124.     sem_destroy(&sem2);
  125.     pthread_exit(0);
  126. }


  127. void *thread3_func(void *arg)
  128. {
  129.     int reg = 0;
  130.     sem_wait(&sem3);
  131.     if(reg != 0)
  132.     {
  133.         perror("fail to wait sem1\n");
  134.         exit(EXIT_FAILURE);
  135.     }
  136.     printf("%s", (char*)arg);
  137.     flush(stdout);
  138.     sem_destroy(&sem3);
  139.     pthread_exit(0);
  140. }


阅读(1833) | 评论(0) | 转发(0) |
0

上一篇:互斥量

下一篇:linux常见的内存错误

给主人留下些什么吧!~~