首页 > 代码库 > thread_25

thread_25

#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 startmain get  lock(1)lock wrlockstart thread (1)thread (1) end*/

 

thread_25