Read-modify-write

讀-修改-寫(read-modify-write)是電腦科學中的一個原子操作(atomic operation,類似的還有test-and-set, fetch-and-add, compare-and-swap等),操作過程是讀一個主記憶體位置(或IO埠),修改其值,再寫回原位置。[1][2]

必須要先讀操作的一個原因是,系統架構往往只允許字(word)級的讀寫,必須先讀出那些不做修改的位元,保持不變再寫回。寫成C語言陳述式類似於:

*pRegister |= SOME_BIT;

Read-modify-write指令用於IO埠時,可能會產生出乎意料的結果,如無法給一個位元置位。這往往是因為寫操作並不影響到讀操作的源暫存器。[3]

RAID也使用這一術語描述原子操作的read-modify-write序列。[4] Such RAID levels include RAID 4, RAID 5 and RAID 6.

參見

編輯

參考文獻

編輯
  1. ^ "Writing Lock-Free Code: A Corrected Queue". Dr. Dobb's Journal. (頁面存檔備份,存於互聯網檔案館) by Herb Sutter: "Compare-and-swap (CAS) is ... widely available ... However, some systems instead provide the equivalently powerful load-linked/store-conditional (LL/SC) instead."
  2. ^ Herlihy, Maurice. Wait-free synchronization (PDF). ACM Transactions on Programming Languages and Systems. January 1991, 13 (1): 124–149 [2007-05-20]. doi:10.1145/114005.102808. (原始內容存檔 (PDF)於2011-06-05). 
  3. ^ Massmind: "The read–modify–write problem". techref.massmind.org. [2017-11-22]. (原始內容存檔於2017-12-01). 
  4. ^ Basic RAID Organizations. umass.edu. [2013-10-04]. (原始內容存檔於2013-10-24).