Linux --多线程之读写锁1

来源:互联网 发布:广东影响力网络盛典 编辑:程序博客网 时间:2024/06/06 12:38
#include "apue.h"/**读写锁:和互斥量类似 然读写锁并行性更高 如,对于一个变量的读取是可以多个线程并行的读写锁有三种状态 读模式下的加锁,写模式下的加锁,不加锁。一次只有一个线程可以占有写模式下的读写锁 但是多个线程可以同时读模式下的读写锁1.读写锁在写加锁状态时,在它被解锁前,试图对这个锁加锁的线程都会阻塞 2.读写锁在读加锁状态时 所有试图以读模式 对其加锁的线程都会获得访问权 ,但是线程以写模式对读锁加锁 必须等到所有线程都释放锁,此时会阻塞所有随后的读模式加锁请求 直到写锁释放读写锁使用前必须初始化  使用后必须销毁读锁1 如下示例 两个for 循环 不同的ar交叉打印 表明拥有统一把读锁的两个线程可以交叉执行*/pthread_rwlock_t rwlock;void *thread_fun1(void *ar){    //加锁     pthread_rwlock_rdlock(&rwlock);int i;for(i=0;i<9;i++){printf("==================== i=%d  fun1 ar=%d \n",i,(int)ar);usleep(1000);}//解锁 后面执行 其他线程可以争夺pthread_rwlock_unlock(&rwlock);printf("thread1 has finished\n");return (void *)1;}void *thread_fun2(void *ar){    //加锁    pthread_rwlock_rdlock(&rwlock);int i;for(i=0;i<9;i++){printf(" i=%d  fun2 ar=%d\n",i,(int)ar);usleep(1000);}//解锁 后面执行 其他线程可以争夺pthread_rwlock_unlock(&rwlock);printf("thread2 has finished\n");return (void *)1;} int main(){pthread_t tid1,tid2;int err1,err2,err_rwl,th_join1,th_join2;//初始化互斥量err_rwl = pthread_rwlock_init(&rwlock,NULL);if(err_rwl!=0){perror("rwlock failure");return -1;}//创建线程err1 = pthread_create(&tid1,NULL,thread_fun1,(void *)1);err2 = pthread_create(&tid2,NULL,thread_fun2,(void *)2);if(err1||err2){//err1或者err2不等于0时perror(" fail to create thread ");return -1;}printf("success to create thread err1 =%d err2 =%d\n",err1,err2);th_join1 = pthread_join(tid1,NULL);th_join2 = pthread_join(tid2,NULL);printf("thread1 and thread2 has finished\n");if(th_join1 || th_join2){perror("fail to join thread");return -1;}//注销锁pthread_rwlock_destroy(&rwlock);}


#include "apue.h"/**读写锁:和互斥量类似 然读写锁并行性更高 如,对于一个变量的读取是可以多个线程并行的读写锁有三种状态 读模式下的加锁,写模式下的加锁,不加锁。一次只有一个线程可以占有写模式下的读写锁 但是多个线程可以同时读模式下的读写锁1.读写锁在写加锁状态时,在它被解锁前,试图对这个锁加锁的线程都会阻塞 2.读写锁在读加锁状态时 所有试图以读模式 对其加锁的线程都会获得访问权 ,但是线程以写模式对读锁加锁 必须等到所有线程都释放锁,此时会阻塞所有随后的读模式加锁请求 直到写锁释放读写锁使用前必须初始化  使用后必须销毁写锁1 如下示例 两个for 循环 不同的ar 不能交叉打印 表明拥有同一把读锁的两个线程不能交叉执行*/pthread_rwlock_t rwlock;void *thread_fun1(void *ar){//加锁 保证加锁部分 同一时间只能有一个线程访问    pthread_rwlock_wrlock(&rwlock);int i;for(i=0;i<9;i++){printf("==================== i=%d  fun1 ar=%d \n",i,(int)ar);usleep(1000);}//解锁 后面执行 其他线程可以争夺pthread_rwlock_unlock(&rwlock);printf("thread1 has finished\n");return (void *)1;}void *thread_fun2(void *ar){//加锁 保证加锁部分 同一时间只能有一个线程访问    pthread_rwlock_wrlock(&rwlock);int i;for(i=0;i<9;i++){printf(" i=%d  fun2 ar=%d\n",i,(int)ar);usleep(1000);}//解锁 后面执行 其他线程可以争夺pthread_rwlock_unlock(&rwlock);printf("thread2 has finished\n");return (void *)1;} int main(){pthread_t tid1,tid2;int err1,err2,err_rwl,th_join1,th_join2;//初始化互斥量err_rwl = pthread_rwlock_init(&rwlock,NULL);if(err_rwl!=0){perror("rwlock failure");return -1;}//创建线程err1 = pthread_create(&tid1,NULL,thread_fun1,(void *)1);err2 = pthread_create(&tid2,NULL,thread_fun2,(void *)2);if(err1||err2){//err1或者err2不等于0时perror(" fail to create thread ");return -1;}printf("success to create thread err1 =%d err2 =%d\n",err1,err2);th_join1 = pthread_join(tid1,NULL);th_join2 = pthread_join(tid2,NULL);printf("thread1 and thread2 has finished\n");if(th_join1 || th_join2){perror("fail to join thread");return -1;}//注销锁pthread_rwlock_destroy(&rwlock);}


0 0
原创粉丝点击