?讀共享,寫獨享,寫鎖優先級高
POSIX 定義的讀寫鎖的數據類型是: pthread_rwlock_t
1、初始化讀寫鎖
#include<pthread.h>
int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);可以使用宏 PTHREAD_RWLOCK_INITIALIZER 靜態初始化讀寫鎖,比如:
pthread_rwlock_t my_rwlock = PTHREAD_RWLOCK_INITIALIZER;
這種方法等價于使用 NULL 指定的 attr 參數調用 pthread_rwlock_init() 來完成動態初始化,不同之處在于PTHREAD_RWLOCK_INITIALIZER 宏不進行錯誤檢查。
功能:
????????用來初始化 rwlock 所指向的讀寫鎖。
參數:
????????rwlock:指向要初始化的讀寫鎖指針。
????????attr:讀寫鎖的屬性指針。如果 attr 為NULL 則會使用默認的屬性初始化讀寫鎖,否則
? ? ? ? ? ? ? 使用指定的 attr 初始化讀寫鎖。
返回值:
????????成功:0,讀寫鎖的狀態將成為已初始化和已解鎖。
????????失敗:非 0 錯誤碼
2、釋放讀寫鎖
#include<pthread.h>
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
功能:
????????用于銷毀一個讀寫鎖,并釋放所有相關聯的資源(所謂的所有指的是由pthread_rwlock_init()
????????自動申請的資源) 。
參數:
????????rwlock:讀寫鎖指針。
返回值:
? ??????成功:0
????????失敗:非 0 錯誤碼
3、申請讀鎖
#include<pthread.h>
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
功能:
????????以阻塞方式在讀寫鎖上獲取讀鎖(讀鎖定)。
????????如果沒有寫者持有該鎖,并且沒有寫者阻塞在該鎖上,則調用線程會獲取讀鎖。
????????如果調用線程未獲取讀鎖,則它將阻塞直到它獲取了該鎖。一個線程可以在一個讀寫鎖上多次執行
????????讀鎖定。線程可以成功調用 pthread_rwlock_rdlock() 函數n 次,但是之后該線程必須調用
? ? ? ? pthread_rwlock_unlock() 函數 n 次才能解除鎖定。
參數:
????????rwlock:讀寫鎖指針。
返回值:
????????成功:0
????????失敗:非 0 錯誤碼
4、申請寫鎖
#include<pthread.h>
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
功能:
????????在讀寫鎖上獲取寫鎖(寫鎖定)。
????????如果沒有寫者持有該鎖,并且沒有寫者讀者持有該鎖,則調用線程會獲取寫鎖。
????????如果調用線程未獲取寫鎖,則它將阻塞直到它獲取了該鎖。
參數:
????????rwlock:讀寫鎖指針。
返回值:
????????成功:0
????????失敗:非 0 錯誤碼
5、釋放讀寫鎖
#include<pthread.h>
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
功能:
????????無論是讀鎖或寫鎖,都可以通過此函數解鎖。
參數:
????????rwlock:讀寫鎖指針。
返回值:
????????成功:0
????????失敗:非 0 錯誤碼
示例代碼
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>?
//定義一把鎖
pthread_rwlock_t rwlock;??void* read_data01(void* arg)
{
????????int *p = (int *)arg;
?????????while (1)
?????????{
????????????????//申請上讀鎖
????????????????pthread_rwlock_rdlock(&rwlock);
????????????????printf("任務A:num=%d\n", *p);
????????????????//解讀寫鎖
????????????????pthread_rwlock_unlock(&rwlock);
????????????????sleep(1);?
?????????}
?????????return NULL;
}void* read_data02(void* arg)
{
?????int *p = (int *)arg;
????while (1)
????{
?????????//申請上讀鎖
? ? ? ? ?pthread_rwlock_rdlock(&rwlock);
? ? ? ? ?printf("任務B:num=%d\n", *p);?
? ? ? ? ?//解讀寫鎖?
? ? ? ? ?pthread_rwlock_unlock(&rwlock);
? ? ? ? ?sleep(1);
????? ?}?
? ? return NULL;?
?}void* write_data(void* arg)
{?
????int *p = (int *)arg;
? ? while (1)
? ?{
????? ?//申請寫鎖?
????? pthread_rwlock_wrlock(&rwlock);?
????? (*p)++;?
????? //解讀寫鎖?
????? pthread_rwlock_unlock(&rwlock);?
????? printf("任務C:寫入num=%d\n", *p);?
????? sleep(2);?
????? }?
? ????return NULL;?
?}int main(int argc,char const*argv[])?
{?
?????//定義一個公共資源
? ? ?int num = 0;
?????//初始化一把鎖
????pthread_rwlock_init(&rwlock, NULL);
?????//創建兩個線程
????pthread_t tid1, tid2, tid3;
????pthread_create(&tid1, NULL, read_data01, (void *)&num);//讀
????pthread_create(&tid2, NULL, read_data02, (void *)&num);//讀
????pthread_create(&tid3, NULL, write_data, (void *)&num);//寫
????pthread_join(tid1, NULL);
? ? pthread_join(tid2, NULL);
? ? pthread_join(tid3, NULL);
? ? //銷毀鎖? ?
????pthread_rwlock_destroy(&rwlock);?
? ? return 0;?
}