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

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

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

    so true

    心懷未來,開創未來!
    隨筆 - 160, 文章 - 0, 評論 - 40, 引用 - 0
    數據加載中……

    read-write lock

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

    測試程序:
    #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;
            // 設置寫優先
            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(); //讓程序暫停,等待鍵盤敲入一個字節后,接著走
        pthread_create(&tid, NULL, thread_routine, (void*)1);
        sleep(2); //確保寫鎖被優先拿到
        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) 評論(0)  編輯  收藏 所屬分類: C&C++Linux

    主站蜘蛛池模板: 羞羞视频在线免费观看| 亚洲av日韩av不卡在线观看| 亚洲一区二区三区精品视频| 毛片在线播放免费观看| 亚洲精品蜜桃久久久久久| 久久久精品视频免费观看 | 一区在线免费观看| www.亚洲精品| 精品多毛少妇人妻AV免费久久| 国产a v无码专区亚洲av | 91热成人精品国产免费| 91亚洲精品视频| 99精品国产免费久久久久久下载| 2017亚洲男人天堂一| 免费观看毛片视频| 牛牛在线精品观看免费正| 老司机亚洲精品影视www| 大地影院MV在线观看视频免费| 亚洲激情在线视频| 四虎国产精品免费久久| 色欲aⅴ亚洲情无码AV蜜桃| 亚洲第一永久AV网站久久精品男人的天堂AV | 中文字幕无码视频手机免费看| 亚洲www在线观看| 国产hs免费高清在线观看| a一级爱做片免费| 久久综合日韩亚洲精品色| av大片在线无码免费| 亚洲精品理论电影在线观看| 亚洲av无码天堂一区二区三区 | 18禁免费无码无遮挡不卡网站| 亚洲男人的天堂网站| 亚洲国产精品不卡毛片a在线| 日韩成人免费视频| 亚洲精品女同中文字幕| 亚洲精品国产美女久久久| 日韩精品无码区免费专区| xxxxx做受大片视频免费| 亚洲日产2021三区| 亚洲日韩中文在线精品第一| 99爱在线精品视频免费观看9|