码迷,mamicode.com
首页 > 其他好文 > 详细

thread_25

时间:2014-09-27 00:40:48      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   sp   div   art   c   

#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <sched.h>
#include<string.h>
pthread_rwlock_t mutex;     
void *thread1(void *arg)
 {
       printf("start thread (1)\n");
       sleep(2);
      // pthread_rwlock_init(&mutex, NULL);//如果把这一行注释掉  将不会产生死锁
       pthread_rwlock_unlock(&mutex);
       printf("thread (1) end\n");
}

 int main(int argc, char *argv[])
 {       
         pthread_t  t1, t2, t3;
         int ret, i;
         printf("main start\n");
         pthread_rwlock_init(&mutex, NULL);
         if(!pthread_rwlock_rdlock(&mutex))
                 printf("main get  lock(1)\n");
        pthread_create(&t1, NULL, thread1, NULL);
         printf("lock wrlock\n");
         if(!pthread_rwlock_wrlock(&mutex))
              printf("main get  lock(2)\n");
        pthread_rwlock_unlock(&mutex);
         pthread_join(t1, NULL);
         printf("main end\n");
      return 0;
 } 
/*运行结果:最终在36行出现死锁。如下:
 main start
main get  lock(1)
lock wrlock
start thread (1)
thread (1) end*/

 

thread_25

标签:style   blog   color   io   ar   sp   div   art   c   

原文地址:http://www.cnblogs.com/leijiangtao/p/3995852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!