Linux --多线程之读写锁2

    xiaoxiao2021-12-14  45

    #include "apue.h" /** 读写锁:和互斥量类似 然读写锁并行性更高 如,对于一个变量的读取是可以 多个线程并行的 读写锁有三种状态 读模式下的加锁,写模式下的加锁,不加锁。 一次只有一个线程可以占有写模式下的读写锁 但是多个线程可以同时读模式下的读写锁 读写锁在写加锁状态时,在它被解锁前,试图对这个锁加锁的线程都会阻塞 读写锁在读加锁状态时 所有试图以读模式 对其加锁的线程都会获得访问权 ,但是线程以写模式对 读锁加锁 必须等到所有线程都释放锁,此时会阻塞所有随后的读模式加锁请求 直到写锁释放 读写锁使用前必须初始化 使用后必须销毁 读锁2 如下示例 被锁住的for循环 中的四个输出没有一直连续输出并且 ar也没有连续相同 表示被锁期间其他线程也能访问 */ pthread_rwlock_t rwlock; //共享资源 int i=0; void *thread_fun1(void *ar){ //加锁 pthread_rwlock_rdlock(&rwlock); for(;i<5;i++) { printf("==================== 1 i=%d fun1 ar=%d\n",i,(int)ar); usleep(1000*500); printf("==================== 2 i=%d fun1 ar=%d\n",i,(int)ar); usleep(1000*500); printf("==================== 3 i=%d fun1 ar=%d\n",i,(int)ar); usleep(1000*500); printf("==================== 4 i=%d fun1 ar=%d\n\n",i,(int)ar); } //解锁 后面执行 其他线程可以争夺 pthread_rwlock_unlock(&rwlock); printf("thread1 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_fun1,(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" /** 读写锁:和互斥量类似 然读写锁并行性更高 如,对于一个变量的读取是可以 多个线程并行的 读写锁有三种状态 读模式下的加锁,写模式下的加锁,不加锁。 一次只有一个线程可以占有写模式下的读写锁 但是多个线程可以同时读模式下的读写锁 读写锁在写加锁状态时,在它被解锁前,试图对这个锁加锁的线程都会阻塞 读写锁在读加锁状态时 所有试图以读模式 对其加锁的线程都会获得访问权 ,但是线程以写模式对 读锁加锁 必须等到所有线程都释放锁,此时会阻塞所有随后的读模式加锁请求 直到写锁释放 读写锁使用前必须初始化 使用后必须销毁 写锁2 如下示例 被锁住的for循环 中的四个输出一直连续输出并且ar相同 表示被锁期间只有一个线程能够访问资源 */ pthread_rwlock_t rwlock; //共享资源 int i=0; void *thread_fun1(void *ar){ //加锁 保证加锁部分 同一时间只能有一个线程访问 pthread_rwlock_wrlock(&rwlock); for(;i<5;i++) { printf("==================== 1 i=%d fun1 ar=%d\n",i,(int)ar); usleep(1000*500); printf("==================== 2 i=%d fun1 ar=%d\n",i,(int)ar); usleep(1000*500); printf("==================== 3 i=%d fun1 ar=%d\n",i,(int)ar); usleep(1000*500); printf("==================== 4 i=%d fun1 ar=%d\n\n",i,(int)ar); } //解锁 后面执行 其他线程可以争夺 pthread_rwlock_unlock(&rwlock); printf("thread1 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_fun1,(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); }

    转载请注明原文地址: https://ju.6miu.com/read-970700.html

    最新回复(0)