TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 230 subscribers
Post #25384 9
Performance Battle: Mutex vs CAS vs TAS vs Intel TSX


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.

int sumcriticalsection;
std::atomic<int> sumatomic;

void Thread() {
constexpr auto kLoop{ 2200'0000 };
constexpr auto kNumThread{ 88 };
   
for (int i = 0; i < kLoop / kNumThread; ++i) {
        if (TryAcquire()) {
            sum
criticalsection += 1;
            Release();
        } else {
            sum
atomic.fetchadd(1, std::memoryorder::relaxed);
        }

Idle(idletime);
    }
}

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.

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



The experiments were conducted on a two-node NUMA system. Accordingly, both
sumcriticalsection and sumatomic` 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

||sum\
critical_section|sum_atomic|elapsed_seconds|
|:-|:-|:-|:-|
|Mutex|478K|21.5M|1.618|
|CAS|557K|21.4M|0.569|
|TAS|564K|21.4M|0.567|
|TSX|746K|21.2M|0.492|


Low Contention: idle time = 3.0 us

||sum_critical_section|sum_atomic|elapsed_seconds|
|:-|:-|:-|:-|
|Mutex|915K|21.1M|1.835|
|CAS|1.90M|20.1M|1.254|
|TAS|1.95M|20.0M|1.264|
|TSX|10.2M|11.7M|1.142|


The winner of this benchmark is Intel TSX.

Of course, a benchmark win doesn't automatically make TSX superior in every situation. That said, it did win this round.

(when the idle time was zero, TSX, CAS, and TAS achieved nearly identical elapsed times, whereas std::mutex was consistently slower.)



What are your thoughts on this matchup?


=

https://redd.it/1tzey4h
@r_cpp
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
More from @r_cpp
  1. Sep 29, 2026Boost.Graph 1.95 will be C++17 Dear Boost.Graph community, In two release cycles (Boost re…
  2. Sep 26, 2026Token Sequence Injection & Modern Macros: The Most Game-Changing Compile-Time Feature in C…
  3. Sep 25, 2026myStringStream.str("") Considered Harmful Under C++20 I was recently looking at some (rath…
  4. Sep 20, 2026A clever branch free optimization I'm the developer of memlz which is an extremely fast co…
  5. Sep 15, 2026Inside Boost.PolyCollection https://bannalia.blogspot.com/2026/09/inside-boostpolycollecti…
  6. Sep 11, 2026C++26: Standard Library Hardening Experiments https://www.cppstories.com/2026/hardening-ex…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →