Performance Battle:
Mutex vs CAS vs TAS vs Intel TSX
std::mutex: A standard C++ lock object that provides mutual exclusion between threads.
CAS (Compare-And-Swap): An atomic operation that updates a memory location only if its current value matches an expected value.
TAS (Test-And-Set): An atomic operation that reads and sets a value simultaneously.
Intel TSX (Transactional Synchronization Extensions): An Intel technology that uses hardware transactional memory to reduce lock contention.
The following algorithm uses multiple threads to add 1 to a shared memory variable kLoop times. In this case, the sum of sum_atomic and sum_critical_section will be equal to kLoop. Although this is a highly inefficient algorithm, let's just accept it. (just for fun!)
int sum_critical_section;
std::atomic<int> sum_atomic;
void Thread() {
constexpr auto kLoop{ 2200'0000 };
constexpr auto kNumThread{ 88 };
for (int i = 0; i < kLoop / kNumThread; ++i) {
if (TryAcquire()) {
sum_critical_section += 1;
Release();
} else {
sum_atomic.fetch_add(1, std::memory_order::relaxed);
}
Idle(idle_time);
}
}
An idle period was inserted between work units to control the level of contention.
(high contention: 0.6 us / low contention: 3.0 us)
TryAcquire are implemented as follows.
- Mutex
return mx.try_lock();
2. CAS
return not atomic_bool.load(std::memory_order::relaxed) and
atomic_bool.compare_exchange_strong(expected, true, std::memory_order::acquire, std::memory_order::relaxed)); // expected = false
3. TAS
return not (atomic_flag.test(std::memory_order::relaxed) or atomic_flag.test_and_set(std::memory_order::acquire));
4. Intel TSX
return _xbegin() == _XBEGIN_STARTED;
For both CAS and TAS, the lock variable is checked before attempting the atomic operation. If the lock is already set (true), the function immediately returns false without performing the CAS or TAS operation. Otherwise, performance will degrade.
System Description
| CPU | 2 × Intel Xeon E5-2696 v4 (total 88-thread) |
|---|
| Build | C++23, g++ 13.3.0, -Ofast |
| OS | Ubuntu Server 24.04 |
The experiments were conducted on a two-node NUMA system. Accordingly, both sum_critical_section and sum_atomic were split into two separate counters.
Which of these four approaches do you think will win: Mutex, CAS, TAS, or Intel TSX?
Let's keep the rules simple: the winner is whichever finishes the workload the fastest.
.
.
.
.
.
.
.
.
High Contention: idle time = 0.6 us
| Algorithm | sum_critical_section | sum_atomic | elapsed seconds |
|---|
| Mutex | 1.04M | 21.0M | 0.521 |
| CAS | 2.06M | 19.9M | 0.333 |
| TAS | 2.00M | 20.0M | 0.335 |
| TSX | 746K | 21.2M | 0.492 |
Low Contention: idle time = 3.0 us
| Algorithm | sum_critical_section | sum_atomic | elapsed seconds |
|---|
| Mutex | 6.93M | 15.1M | 1.216 |
| CAS | 8.51M | 13.5M | 1.122 |
| TAS | 8.61M | 13.4M | 1.107 |
| TSX | 10.2M | 11.7M | 1.142 |
The winners of this benchmark are CAS and TAS
Of course, a benchmark win doesn't automatically make CAS or TAS superior in every situation. That said, it did win this round.
What are your thoughts on this matchup?
Errata (2026-06-08, 01:50 UTC):
Sorry!
I mentioned that, in the 2-node NUMA environment, I separated sum_critical_section and sum_atomic into two instances. However, I forgot to split the lock variables used for the mutex, CAS, and TAS implementations accordingly.
After rerunning the experiments, the winners are CAS and TAS.
=