Search code examples
c++atomicthread-synchronization

compare and swap vs test and set


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


Solution

  • 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.