多线程编程之顺序锁

一、什么是顺序锁

  顺序锁对读写锁的一种优化,使用顺序锁时,读不会被写执行单元阻塞(在读写锁中,写操作必须要等所有读操作完成才能进行)。也就是说,当向一个临界资源中写入的同时,也可以从此临界资源中读取,即实现同时读写,但是不允许同时写数据。如果读执行单元在读操作期间,写执行单元已经发生了写操作,那么,读执行单元必须重新开始,这样保证了数据的完整性,当然这种可能是微乎其微。顺序锁的性能是非常好的,同时他允许读写同时进行,大大的提高了并发性。

二、顺序锁的缺陷

  顺序锁的缺陷在于,互斥访问的资源不能是指针,因为写操作有可能导致指针失效,而读操作对失效的指针进行操作将会发生意外错误。

  顺序锁在某些场合比读写锁更加高效,但读写锁可以适用于所有场合,而顺序锁不行,所以顺序锁不能完全替代读写锁

三、顺序锁的实现

  在Linux内核中,有顺序锁的实现方案:

复制代码
typedef struct {
    unsigned sequence;    /* 顺序计数器 */
    spinlock_t lock;
} seqlock_t;

static inline void write_seqlock(seqlock_t *sl)
{
    spin_lock(&sl->lock);
    ++sl->sequence;
    smp_wmb();
}

static inline void write_sequnlock(seqlock_t *sl)
{
    smp_wmb();
    sl->sequence++;
    spin_unlock(&sl->lock);
}

static __always_inline unsigned read_seqbegin(const seqlock_t *sl)
{
    unsigned ret;

repeat:
    ret = ACCESS_ONCE(sl->sequence);
    if (unlikely(ret & 1)) {
        cpu_relax();
        goto repeat;
    }
    smp_rmb();
    return ret;
}

 /*
  * Test if reader processed invalid data.
  *
  * If sequence value changed then writer changed data while in section.
  */
static __always_inline int read_seqretry(const seqlock_t *sl, unsigned start )
{
    smp_rmb();
    return unlikely(sl->sequence != start);
}
复制代码

 四、顺序锁的用法

  顺序锁的写操作单元执行如下代码:

write_seqlock(&seqlock);
    write_something();    // 写操作代码块
write_sequnlock(&seqlock);

   顺序锁的读操作单元执行如下代码:

do{
    seqnum = read_seqbegin(&seqlock);  // 读执行单元在访问共享资源时要调用该函数,返回锁seqlock的顺序号 
    read_something(); // 读操作代码段 
} while( read_seqretry(&seqlock, seqnum)); // 在读结束后调用此函数来检查,是否有写执行单元对资源进行操作,若有则重新读。

 五、Windows平台下的一种实现方式

  参考《多线程的那点儿事(之顺序锁)》文章内容,整理了Windows平台下的一种顺序锁实现方式。代码如下:

复制代码
typedef struct _SEQUENCE_LOCK
{
    unsigned int sequence;
    HANDLE hLock;
}SEQUENCE_LOCK;

unsigned int get_lock_begin(SEQUENCE_LOCK* hSeqLock)
{
    assert(NULL != hSeqLock);

    return hSeqLock->sequence;    
}   
 
int get_lock_retry(SEQUENCE_LOCK* hSeqLock, unsigned int value)
{
    unsigned int new_value;
    assert(NULL != hSeqLock);

    new_value = hSeqLock->sequence;
    return (new_value & 0x1) || (new_value ^ value);    
}

void get_write_lock(SEQUENCE_LOCK* hSeqLock)
{
    assert(NULL != hSeqLock);

    WaitForSingleObject(hSeqLock->hLock);
    hSeqLock->sequence ++;
} 

void release_write_lock(SEQUENCE_LOCK* hSeqLock)
{
    assert(NULL != hSeqLock);

    hSeqLock->sequence ++;
    ReleaseMutex(hSeqLock->hLock);
}
复制代码

  使用时候的方法类似,参考如下代码:

复制代码
void read_process(SEQUENCE_LOCK* hSeqLock)
{
    unsigned int sequence;

    do{
       sequence = get_lock_begin(hSeqLock);
       /* read operation  */
    }while(get_lock_retry(hSeqLock, sequence));
}

void write_process(SEQUENCCE_LOCK* hSeqLock)
{
    get_write_lock(hSeqLock);
    /* write operation */
    release_write_lock(hSeqLock);
}
复制代码

  可以看出,这里的顺序锁原理和用法也是一样的。

小结:

  1. 读锁退出有两种情况:写操作正在进行;或者没有写锁
  2. 写锁之间需要互斥操作
  3. 互斥操作的资源不能是指针,否则有可能在访问的时候会造成异常,因为有可能边写边读
  4. 顺序锁代替不了读写锁,因为读写锁可以保证所有的数据操作,而顺序锁不行
原文地址:https://www.cnblogs.com/cyyljw/p/8006857.html