Linux 讀寫鎖

來源:互聯網
上載者:User
/*功能,Linux 讀寫鎖部落格,http://blog.csdn.net/shunqiziranhao007/article/details/8700288日期,2013年3月21日*/#include <stdio.h>#include <pthread.h>#include <unistd.h>// 互斥量只允許一個線程對其加鎖,所以並行性不高pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;int gcount1 = 0;void* tf1(void *p){pthread_mutex_lock(&mutex);++gcount1;printf("tf1 write, gcount1 is %d\n", gcount1);// 拿到mutex後,等待1s看看其他需要mutex的線程是否會等待1ssleep(1);pthread_mutex_unlock(&mutex);return 0;}void* tf2(void *p){pthread_mutex_lock(&mutex);printf("tf2 read, gcount1 is %d\n", gcount1);// 拿到mutex後,等待1s看看其他需要mutex的線程是否會等待1ssleep(1);pthread_mutex_unlock(&mutex);return 0;}void* tf3(void *p){pthread_mutex_lock(&mutex);printf("tf3 read, gcount1 is %d\n", gcount1);// 拿到mutex後,等待1s看看其他需要mutex的線程是否會等待1ssleep(1);pthread_mutex_unlock(&mutex);return 0;}// 看看使用讀寫鎖的效果,一次只有一個線程佔有寫入模式的讀寫鎖,但是可以有// 多個線程同時佔有讀模式的讀寫鎖。pthread_rwlock_t lock;int gcount2;void* tf4(void *p){pthread_rwlock_wrlock(&lock);++gcount2;printf("tf4 write, gcount2 is %d\n", gcount2);// 拿到rwlock後,等待1s看看其他需要rwlock的線程是否會等待1ssleep(1);pthread_rwlock_unlock(&lock);return 0;}void* tf5(void *p){pthread_rwlock_rdlock(&lock);printf("tf5 read, gcount2 is %d\n", gcount2);// 拿到rwlock後,等待1s看看其他需要rwlock的線程是否會等待1ssleep(1);pthread_rwlock_unlock(&lock);return 0;}void* tf6(void *p){pthread_rwlock_rdlock(&lock);printf("tf6 read, gcount2 is %d\n", gcount2);// 拿到rwlock後,等待1s看看其他需要rwlock的線程是否會等待1ssleep(1);pthread_rwlock_unlock(&lock);return 0;}void* tf7(void *p){pthread_rwlock_wrlock(&lock);++gcount2;printf("tf7 write, gcount2 is %d\n", gcount2);// 拿到rwlock後,等待1s看看其他需要rwlock的線程是否會等待1ssleep(1);pthread_rwlock_unlock(&lock);return 0;}int main(){pthread_t tid1, tid2, tid3, tid4, tid5, tid6, tid7;pthread_create(&tid1, 0, tf1, 0);pthread_create(&tid2, 0, tf2, 0);pthread_create(&tid3, 0, tf3, 0);pthread_join(tid1, 0);pthread_join(tid2, 0);pthread_join(tid3, 0);// 由上得// 一次只能讓一個線程對mutex上鎖,其他請求等待,這樣相當於串列執行了pthread_rwlock_init(&lock, 0);pthread_create(&tid4, 0, tf4, 0);pthread_create(&tid5, 0, tf5, 0);pthread_create(&tid6, 0, tf6, 0);pthread_create(&tid7, 0, tf7, 0);pthread_join(tid4, 0);pthread_join(tid5, 0);pthread_join(tid6, 0);pthread_join(tid7, 0);pthread_rwlock_destroy(&lock);// 由上得// 加了寫鎖後只能有一個線程執行,其他等待。// 同時請求讀寫鎖,請求寫的線程先擷取鎖。// 多個線程可以同時擷取讀鎖return 0;}/*$ ./rwlock.exe tf1 write, gcount1 is 1tf2 read, gcount1 is 1tf3 read, gcount1 is 1tf4 write, gcount2 is 1tf7 write, gcount2 is 2tf6 read, gcount2 is 2tf5 read, gcount2 is 2*/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.