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

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

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

    so true

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

    read-write lock

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

    測(cè)試程序:
    #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è)置寫(xiě)優(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(); //讓程序暫停,等待鍵盤(pán)敲入一個(gè)字節(jié)后,接著走
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        sleep(2); //確保寫(xiě)鎖被優(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 閱讀(204) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): C&C++ 、Linux

    主站蜘蛛池模板: 国产亚洲婷婷香蕉久久精品| 亚洲国产精品成人综合色在线| 一边摸一边桶一边脱免费视频 | 国产亚洲综合成人91精品| 亚洲日韩精品无码AV海量| 久久aa毛片免费播放嗯啊| 亚洲av区一区二区三| 色噜噜亚洲男人的天堂| 欧洲人成在线免费| 亚洲国产精品自产在线播放| 亚洲人成综合网站7777香蕉| 一级毛片aaaaaa免费看| 中文字幕亚洲不卡在线亚瑟| mm1313亚洲国产精品无码试看| 猫咪社区免费资源在线观看 | 国产AV无码专区亚洲AV蜜芽| 18禁男女爽爽爽午夜网站免费| 亚洲一区AV无码少妇电影☆| 羞羞视频免费网站含羞草| 黄色成人网站免费无码av| 18亚洲男同志videos网站| 99re6在线精品免费观看| 亚洲福利精品一区二区三区 | 秋霞人成在线观看免费视频| 中文字幕无码精品亚洲资源网| 国产AV无码专区亚洲AV麻豆丫| 免费无码又黄又爽又刺激| 亚洲婷婷综合色高清在线| 999久久久免费精品播放| 亚洲精品国产美女久久久| 一级做a爰性色毛片免费| 国产一区二区三区免费在线观看| 色婷五月综激情亚洲综合| 91久久精品国产免费直播| 久久精品国产亚洲av日韩| 日韩视频免费在线观看| 国产亚洲成av片在线观看| 一级一级毛片免费播放| 亚洲国产综合精品一区在线播放| 怡红院亚洲红怡院在线观看| 国产精品成人无码免费|