<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    so true

    心懷未來,開創(chuàng)未來!
    隨筆 - 160, 文章 - 0, 評論 - 40, 引用 - 0
    數(shù)據(jù)加載中……

    read-write lock

    (一)當(dāng)前被讀鎖占著
        1。如果讀優(yōu)先(默認(rèn)是PTHREAD_RWLOCK_PREFER_READER_NP,即讀優(yōu)先),那么等著的讀請求可以快速拿到鎖,寫一直被餓著(直到?jīng)]有任何等待獲取讀鎖的情況下,寫才能拿到寫鎖);
        2。如果寫優(yōu)先,那么讀和寫都等著,一但前面的讀釋放了鎖,寫立刻就會優(yōu)先讀來拿到鎖;
    (二)當(dāng)前被寫鎖占著
        不論誰優(yōu)先,都需要把所有等著的寫都服務(wù)完,才會給讀機(jī)會,所以寫多讀少的情況下,就應(yīng)該用互斥鎖了;

    測試程序:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    #include <stdint.h>
    #include <pthread.h>
    #include <vector>
    #include <map>
    #include <set>
    using namespace std;
    long g_idx = 0;
    class ThreadRwLock {
    private:
        pthread_rwlock_t m_rw_mutex;
    public:
        ThreadRwLock() {
            pthread_rwlockattr_t attr;
            // 設(shè)置寫優(yōu)先
            pthread_rwlockattr_init(&attr);
            //pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
            pthread_rwlock_init(&m_rw_mutex, &attr);
        }
        ~ThreadRwLock() {
            pthread_rwlock_destroy(&m_rw_mutex);
        }
        void rdlock() {
            pthread_rwlock_rdlock(&m_rw_mutex);
        }
        void wrlock() {
            pthread_rwlock_wrlock(&m_rw_mutex);
        }
        int tryrdlock() {
            return pthread_rwlock_tryrdlock(&m_rw_mutex);
        }
        int trywrlock() {
            return pthread_rwlock_trywrlock(&m_rw_mutex);
        }
        void unlock() {
            pthread_rwlock_unlock(&m_rw_mutex);
        }
    };
    ThreadRwLock g_lock;
    void* thread_routine(void* arg) {
        long is_write = (long)arg;
        if (is_write) {
            printf("%lu wait write lock\n", pthread_self()); fflush(stdout);
            g_lock.wrlock();
            long idx = __sync_add_and_fetch(&g_idx, 1);
            printf("%lu get write lock, %ld\n", pthread_self(), idx); fflush(stdout);
            sleep(5);
            g_lock.unlock();
        } else {
            printf("%lu wait read lock\n", pthread_self()); fflush(stdout);
            g_lock.rdlock();
            long idx = __sync_add_and_fetch(&g_idx, 1);
            printf("%lu get read lock, %ld\n", pthread_self(), idx); fflush(stdout);
            sleep(5);
            g_lock.unlock();
        }
        return NULL;
    }
    int main(int argc, char* argv[]) {
        pthread_t tid = 0;
        pthread_create(&tid, NULL, thread_routine, (void*)0);
        pthread_create(&tid, NULL, thread_routine, (void*)0);
        pthread_create(&tid, NULL, thread_routine, (void*)0);
        pthread_create(&tid, NULL, thread_routine, (void*)0);
        pthread_create(&tid, NULL, thread_routine, (void*)0);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        getchar(); //讓程序暫停,等待鍵盤敲入一個字節(jié)后,接著走
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        sleep(2); //確保寫鎖被優(yōu)先拿到
        pthread_create(&tid, NULL, thread_routine, (void*)0);
        pthread_create(&tid, NULL, thread_routine, (void*)0);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)0);
        pthread_create(&tid, NULL, thread_routine, (void*)0);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        pthread_create(&tid, NULL, thread_routine, (void*)0);
        pthread_create(&tid, NULL, thread_routine, (void*)0);
        getchar();
        return 0;
    }

    posted on 2018-05-14 12:07 so true 閱讀(213) 評論(0)  編輯  收藏 所屬分類: C&C++Linux

    主站蜘蛛池模板: 亚洲啪AV永久无码精品放毛片 | 2020久久精品亚洲热综合一本| 免费在线观看一级片| 国产成人综合亚洲AV第一页| 午夜不卡AV免费| 国产乱辈通伦影片在线播放亚洲| 亚洲av综合日韩| 亚洲成?v人片天堂网无码| 一级毛片免费全部播放| 亚洲国产主播精品极品网红| 特级aaaaaaaaa毛片免费视频| 免费国产小视频在线观看| 四虎成人精品国产永久免费无码| 亚洲av日韩av欧v在线天堂| ssswww日本免费网站片| 亚洲乱码中文字幕综合| 国内精品免费视频精选在线观看| 91久久亚洲国产成人精品性色| 久久99国产乱子伦精品免费| 亚洲精品福利网站| 欧美大尺寸SUV免费| 色欲aⅴ亚洲情无码AV蜜桃| 亚洲国产成人五月综合网 | 国产美女视频免费观看的网站| 亚洲色精品aⅴ一区区三区| 国产成人久久AV免费| 亚洲人成影院77777| 日本高清免费不卡在线| 一级成人a免费视频| 亚洲∧v久久久无码精品| 成年网站免费视频A在线双飞| 中日韩亚洲人成无码网站| 亚洲AⅤ永久无码精品AA| 免费女人高潮流视频在线观看| 亚洲中文无码a∨在线观看| 精品国产免费观看| 手机看片国产免费永久| 亚洲91精品麻豆国产系列在线| 亚洲成av人片在线观看天堂无码| 免费高清国产视频| 亚洲成a∨人片在无码2023|