线程顺序

  1. #include<sys/types.h>
  2. #include<stdio.h>
  3. #include<sys/stat.h>
  4. #include<pthread.h>
  5. #include<stdlib.h>
  6. struct{
  7.     pthread_mutex_t mutex;
  8.     char buff[100][100];
  9.     int nput;
  10.     int nval;
  11. }shared={PTHREAD_MUTEX_INITIALIZER};
  12. void *readImage(int i);
  13. int main(void){
  14.     int i;
  15.     pthread_t tache[3];
  16.     
  17.     strcat(shared.buff[0],"***************************");
  18.     strcat(shared.buff[1],"** simuler une image  *****");
  19.     strcat(shared.buff[2],"***************************");
  20.     
  21.     pthread_setconcurrency(3);
  22.     for (i = 0; i < 3; i++) {
  23.         pthread_create(&tache[i], NULL, readImage, i);
  24.     }
  25.     for (i = 0; i < 3; i++) {
  26.         pthread_join(tache[i], NULL);
  27.     }
  28.     exit (0) ;
  29. }
  30. void *
  31. readImage(int i){   
  32.     for ( ; ; ) {
  33.         
  34.         
  35.         if (shared.nput >i) {
  36.             pthread_mutex_unlock(&shared.mutex);
  37.             return(NULL);       
  38.         }
  39.         //verifier si c'est par l'order
  40.         if (shared.nput==i) {
  41.             pthread_mutex_lock(&shared.mutex);
  42.             printf("%s/n", shared.buff[i]);
  43.             sleep(2);
  44.             shared.nput++;
  45.             pthread_mutex_unlock(&shared.mutex);    
  46.         }
  47.         
  48.             
  49.     
  50.         
  51.     }
  52. }

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值