RTT CAS無鎖機制

程序對數據操作流程:

        讀數據   改寫   回寫

   

解決數據一致性問題

 

  1. 主存和獨立工作區的數據一致性

volatile SIndexes _indxes; 主存中線程可見數據(不做緩存機制直接操作主存儲器)

 

 

CachePtrType advance_w()

            {

                SIndexes oldval, newval;

                do

                {

                    oldval._value = _indxes._value; /*Points to a free writable pointer.*/

                    newval._value = oldval._value; /*Points to the next writable pointer.*/

                    // check for full :

                 if ((newval._index[0] == newval._index[1] - 1) || (newval._index[0] == newval._index[1] + _size - 1))

                    {

                        return 0;

                    }

                    newval._index[0]++;

                    if (newval._index[0] >= _size)

                        newval._index[0] = 0;

                    // if ptr is unchanged, replace it with newval.

                } while (!os::CAS(&_indxes._value, oldval._value, newval._value));

                // frome here on :

                // oldval is 'unique', other preempting threads

                // will have a different value for oldval, as

                // _wptr advances. As long as oldval has not been written,

                // rptr will not advance and wptr will remain stuck behind it.

                // return the old position to write to :

                return &_buf[oldval._index[0]];

            }

 

 

CAS(&_indxes._value, oldval._value, newval._value) 爲false:

程序在行過程中,數據區已經讓人串改過,結果已經不可信,那麼繼續執行程序並判。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章