compare and swap vs test and set

余生颓废 提交于 2019-12-03 03:13:52

问题


Could someone explain to me the working and differences of above operations in multi-threading?


回答1:


test-and-set modifies the contents of a memory location and returns its old value as a single atomic operation.

compare-and-swap atomically compares the contents of a memory location to a given value and, only if they are the same, modifies the contents of that memory location to a given new value.

The difference marked in bold.




回答2:


Test and set operates on a bit, compare and swap operates on a 32-bit field.

The z/TPF system favors the use of the test and set (TS) instruction because frequently, lock indicators are bits that are set to control access to critical regions of system code. The test and set (TS) instruction requires fewer registers than the compare and swap (CS) instruction and requires less execution time because only a single byte needs to be set.

and, I found these materials from : http://www.ibm.com/support/knowledgecenter/SSB23S_1.1.0.13/gtpc3/tasinst.html

you can learn more about TSL with the book called "Modern Operating System,Chapter 2"...



来源:https://stackoverflow.com/questions/3659336/compare-and-swap-vs-test-and-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!