首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。