Linux下利用條件變數實現讀寫鎖

來源:互聯網
上載者:User

首先介紹下pthread_cond_t。 在Linux下稱之為狀態變數,與之相關的有下面幾個API:

 

    int pthread_cond_init (pthread_cond_t *COND,pthread_condattr_t *cond_ATTR);
    int pthread_cond_signal (pthread_cond_t *COND);
    int pthread_cond_broadcast (pthread_cond_t *COND);
    int pthread_cond_wait (pthread_cond_t *COND, pthread_mutex_t *MUTEX);
    int pthread_cond_timedwait (pthread_cond_t *COND, pthread_mutex_t *MUTEX, const struct timespec *ABSTIME);
    int pthread_cond_destroy (pthread_cond_t *COND);

 

這裡就講下2個api,pthread_cond_signal和pthread_cond_wait,一般的用法如下:

 

{

    pthread_mutex_lock(lock)

...

    pthread_cond_wait(cond, lock);

...

    pthread_cond_mutex_unlock(lock);

}

 

pthread_cond_wait會解鎖lock,然後在cond上等待,這兩步是atomic operation. 當pthread_cond_wait返回時,會同時對lock上鎖. 這裡我的理解是,如果擷取不到lock鎖,即使cond已經被啟用,pthread_cond_wait依然不會返回。

 

{

    pthread_mutex_lock(lock);

...

    pthread_cond_signal(cond);

...

    pthread_mutex_unlock(lock);

}

 

pthread_cond_signal調用之前一定會先拿到lock鎖。pthread_cond_signal不會去管lock鎖,只是將cond啟用,接下去釋放lock鎖。這時候pthread_cond_wait就可以得到lock從而返回了。

 

 

言歸正傳,下面是利用條件變數實現的一個讀寫鎖的例子:

 

 

typedef struct pthread_rwlock

{

int active_readers; /* -1 when writer lock locked, >0 when read lock locked */

int pending_readers;

int pending_writers;

pthread_mutex_t mutex;

pthread_cond_tok_to_read;

pthread_cond_t ok_to_write;

} pthread_rwlock_t ;

int pthread_rwlock_init(pthread_rwlock_t * lock, pthread_rwlockattr_t *attr){active_readers = 0;pending_readers = 0;pending_writers = 0;pthread_mutex_init(&lock->mutex, NULL);pthread_cond_init(&lock->ok_to_read, NULL);pthread_cond_init(&lock->ok_to_write, NULL);return 0;}int pthread_rwlock_destroy(pthread_rwlock_t * lock) {pthread_mutex_destroy(&lock->mutex);pthread_cond_destroy(&lock->ok_to_read);pthread_cond_destroy(&lock->ok_to_write);return 0;}int pthread_rwlock_rdlock(pthread_rwlock_t * lock) {pthread_mutex_lock(&lock->mutex);lock->pending_readers++;while(lock->active_readers < 0) /* the write lock locked */pthread_cond_wait(&lock->ok_to_read, &lock->mutex);lock->pending_readers--;lock->active_readers++;pthread_mutex_unlock(&lock->mutex);return 0;}int pthread_rwlock_wrlock(pthread_rwlock_t * lock){pthread_mutex_lock(&lock->mutex);lock->pending_writers++;while(lock->active_readers) /* the write lock or read lock locked */pthread_cond_wait(&lock->ok_to_write, &lock->mutex);lock->pending_writers--;lock->active_readers = -1;pthread_mutex_unlock(&lock->mutex);return 0;}int pthread_rwlock_unlock(pthread_rwlock_t * lock){pthread_mutex_lock(&lock->mutex);assert(lock->active_readers)if (lock->active_readers > 0) /* release the read lock */{lock->active_readers--;if (lock->active_readers == 0) /* no read lock locked */{pthread_cond_signal(&lock->ok_to_write);}} else if (lock->active_readers < 0) /* release the write lock */{lock->active_readers=0;/* it may be different, when write lock has higher priority than read lock */if (lock->pending_readers > 0) {pthread_cond_broadcast(&lock->ok_to_read);} else if (lock->pending_writers > 0) {pthread_cond_signal(&lock->ok_to_write);}}pthread_mutex_unlock(&lock->mutex);return 0;}還有pthread_rwlock_tryrdlock 和 pthread_rwlock_trywrlock,感興趣的自己寫吧,感覺這兩個用的不多

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.