1

CAS (compare-and-swap) : boolean compareAndSet(int expect, int update)

FAA(fetch-and-add) : int addAndGet(int delta) ???

TAS (test-and-set) : ???

In my understanding:

CAS (compare-and-swap) "synchronizes" (w/o locks, on CPU instructions level) code like this:

if(a==b) {
  a++;  // or a = a + 7;
}

FAA (fetch-and-add) : "synchronizes" (w/o locks, on CPU instructions level) code like this:

x = x + 7;

But I am not sure about what kind of code "test-and-set" related to.

Code Complete
  • 3,146
  • 1
  • 15
  • 38

1 Answers1

2

Test-and-set is an atomic RMW operation that sets the value at a memory location to 1 and returns the old value (either 1 or 0).

There is no "true" test-and-set operation in Java but you can simulate it by passing 1 to AtomicInteger::getAndSet and expecting it to return 1 or 0. Alternatively, you can simulate TAS by passing true to AtomicBoolean::getAndSet and expecting true or false.

It's not so useful in the example you provided where you increment a variable since TAS is a binary operation.

Eric
  • 906
  • 7
  • 13