1. 程式人生 > >多線程編程之順序鎖

多線程編程之順序鎖

ron b- and 數據操作 重新開始 小結 兩種 static 並發

一、什麽是順序鎖

  順序鎖對讀寫鎖的一種優化,使用順序鎖時,讀不會被寫執行單元阻塞(在讀寫鎖中,寫操作必須要等所有讀操作完成才能進行)。也就是說,當向一個臨界資源中寫入的同時,也可以從此臨界資源中讀取,即實現同時讀寫,但是不允許同時寫數據。如果讀執行單元在讀操作期間,寫執行單元已經發生了寫操作,那麽,讀執行單元必須重新開始,這樣保證了數據的完整性,當然這種可能是微乎其微。順序鎖的性能是非常好的,同時他允許讀寫同時進行,大大的提高了並發性。

二、順序鎖的缺陷

  順序鎖的缺陷在於,互斥訪問的資源不能是指針,因為寫操作有可能導致指針失效,而讀操作對失效的指針進行操作將會發生意外錯誤。

  順序鎖在某些場合比讀寫鎖更加高效,但讀寫鎖可以適用於所有場合,而順序鎖不行,所以順序鎖不能完全替代讀寫鎖

三、順序鎖的實現

  在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. 順序鎖代替不了讀寫鎖,因為讀寫鎖可以保證所有的數據操作,而順序鎖不行

多線程編程之順序鎖