Unix IPC之读写锁

linux中读写锁的rwlock介绍

读写锁比mutex有更高的适用性,可以多个线程同时占用读模式的读写锁,但是只能一个线程占用写模式的读写锁;

1,当读写锁是写加锁状态时, 在这个锁被解锁之前, 所有试图对这个锁加锁的线程都会被阻塞.

2,当读写锁在读加锁状态时, 所有试图以读模式对它进行加锁的线程都可以得到访问权,但是以写模式对它进行枷锁的线程将阻塞;

3,当读写锁在读模式锁状态时, 如果有另外线程试图以写模式加锁, 读写锁通常会阻塞随后的读模式锁请求, 这样可以避免读模式锁长期占用, 而等待的写模式锁请求长期阻塞;

这种锁适用对数据结构进行读的次数比写的次数多的情况下,因为可以进行读锁共享

API接口说明:

1)初始化和销毁

#include <pthread.h>
 
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
 
成功则返回0, 出错则返回错误编号.

 

2)读加锁和写加锁
获取锁的两个函数是阻塞操作
 

#include <pthread.h> 
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock); 
成功则返回0, 出错则返回错误编号.

 

3)非阻塞获得读锁和写锁
非阻塞的获取锁操作, 如果可以获取则返回0, 否则返回错误的EBUSY.

#include <pthread.h>
 
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
 
成功则返回0, 出错则返回错误编号.

 

Unix网络编程-卷2:用互斥锁和信号量实现写者优先的读写锁

typedef struct
{
    pthread_mutex_t   rw_mutex;        /* basic lock on this struct */
    pthread_cond_t    rw_condreaders;  /* for reader threads waiting */
    pthread_cond_t    rw_condwriters;  /* for writer threads waiting */
    int               rw_magic;        /* for error checking */
    int               rw_nwaitreaders; /* the number waiting */
    int               rw_nwaitwriters; /* the number waiting */
    int               rw_refcount;     /* -1:one w_mutex; 0:mutex free; >0:num of r_mutex */
    /* 4-1 if writer has the lock, else # readers holding the lock */
} pthread_rwlock_t;

 

int
pthread_rwlock_rdlock(pthread_rwlock_t *rw)
{
    int     result;

    if (rw->rw_magic != RW_MAGIC) // 判断是否初始化了
        return(EINVAL);

    if ( (result = pthread_mutex_lock(&rw->rw_mutex)) != 0)
        return(result);

    /* 4give preference to waiting writers */
    /**
     * rw->rw_refcount < 0
     * 写者持有读写锁
     *
     * rw->rw_nwaitwriters > 0 (rw->rw_refcount >= 0)
     * 有写者等待
     */
    while (rw->rw_refcount < 0 || rw->rw_nwaitwriters > 0)
    {
        rw->rw_nwaitreaders++;
        result = pthread_cond_wait(&rw->rw_condreaders, &rw->rw_mutex);
        rw->rw_nwaitreaders--;
        if (result != 0)
            break;
    }
    if (result == 0)
        rw->rw_refcount++;      /* another reader has a read lock */

    pthread_mutex_unlock(&rw->rw_mutex);
    return (result);
}
/* end rdlock */
pthread_rwlock_rdlock

 

int
pthread_rwlock_wrlock(pthread_rwlock_t *rw)
{
    int     result;

    if (rw->rw_magic != RW_MAGIC)
        return(EINVAL);

    if ( (result = pthread_mutex_lock(&rw->rw_mutex)) != 0)
        return(result);

    /* 读写锁不可用 */
    while (rw->rw_refcount != 0)
    {
        rw->rw_nwaitwriters++;
        result = pthread_cond_wait(&rw->rw_condwriters, &rw->rw_mutex);
        rw->rw_nwaitwriters--;
        if (result != 0)
            break;
    }
    if (result == 0)
        rw->rw_refcount = -1;

    pthread_mutex_unlock(&rw->rw_mutex);
    return(result);
}
/* end wrlock */
pthread_rwlock_wrlock

 

int
pthread_rwlock_unlock(pthread_rwlock_t *rw)
{
    int     result;

    if (rw->rw_magic != RW_MAGIC)
        return(EINVAL);

    if ( (result = pthread_mutex_lock(&rw->rw_mutex)) != 0)
        return(result);

    if (rw->rw_refcount > 0)
        rw->rw_refcount--;          /* releasing a reader */
    else if (rw->rw_refcount == -1)
        rw->rw_refcount = 0;        /* releasing a writer */
    else
        err_dump("rw_refcount = %d", rw->rw_refcount);

    /* 4give preference to waiting writers over waiting readers */
    if (rw->rw_nwaitwriters > 0) // writers first-------------
    {
        if (rw->rw_refcount == 0)
            result = pthread_cond_signal(&rw->rw_condwriters);
    }
    else if (rw->rw_nwaitreaders > 0)
        result = pthread_cond_broadcast(&rw->rw_condreaders);

    pthread_mutex_unlock(&rw->rw_mutex);
    return(result);
}
/* end unlock */
pthread_rwlock_unlock

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2015-08-14 16:57  指上弹兵赵小括  阅读(298)  评论(0编辑  收藏  举报