【无标题】..........................

 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <unistd.h>
  5 #include <pthread.h>
  6 char a[]="123456789";
  7 pthread_mutex_t mutex1,mutex2;
  8 void* print(void* argv)
  9 {
 10     while(1)
 11     {
 12         pthread_mutex_lock(&mutex1);
 13         printf("a:%s\n",a);
 14         pthread_mutex_unlock(&mutex2);
 15     }
 16 }
 17 void* invert(void* argv)
 18 {
 19     while(1)
 20     {
 21         pthread_mutex_lock(&mutex2);
 22         int i=0,j=strlen(a)-1;
 23         for(;j>i;)
 24         {
 25             char p=a[i];
 26             a[i]=a[j];
 27             a[j]=p;
 28             i++,j--;
 29         }
 30         pthread_mutex_unlock(&mutex1);
 31 
 32     }
 33 
 34 }
 35 int main(int argc, const char *argv[])
 36 {
 37     pthread_mutex_init(&mutex1,NULL);                                                                                                                                                                       
 38     pthread_mutex_init(&mutex2,NULL);
 39     pthread_mutex_lock(&mutex2);
 40     pthread_t tid1;
 41     pthread_t tid2;
 42     if(pthread_create(&tid1,NULL,print,NULL)!=0)
 43     {
 44         printf("tid:%d\n",__LINE__);
 45         return -1;
 46     }
 47     if(pthread_create(&tid2,NULL,invert,NULL)!=0)
 48     {
 49         printf("tid:%d\n",__LINE__);
 50         return -1;
 51     }
 52 
 53     pthread_join(tid1,NULL);
 54     pthread_join(tid2,NULL);
 55     

          pthread_mutex_destroy(&mutex1);                                                                 
 56     pthread_mutex_destroy(&mutex2);

 56 
 57 
 58     return 0;
 59 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值