C++C++11C++14C++17C++20Learn C++Syntax

Learn Strong Compare and Exchange In Modern C++

Learn Strong Compare and Exchange In Modern C++

Since the C++11 standard, the Concurrency Support Library includes built-in support for threads (std::thread) with atomic operations (std::atomic). C++11 provides both weak and strong compare-and-exchange operations in multi-threading applications. Since C++11, strong compare and exchange are used in modern C++ standards such as C++14, C++17, C++20, and in other new standards. In this post, we explain what is strong compare and exchange is with simple examples.

What is strong compare and exchange in C++?

The Strong Compare and Exchange template atomically compares the value pointed to by the atomic object with the value pointed to by expected. This feature comes with C++11 and is used in other modern C++ standards, and performs the following operations based on the comparison results:

  • If the comparison result is true (bitwise-equal), the function replaces the value pointed to by the atomic object with the value desired.
  • If the comparison result is false, the function updates the value in expected with the value pointed by the atomic object.

There are two most common syntaxes, here is the syntax for the compare_exchange_strong, first,

and in strong compare and exchange atomic operations, we can use std::compare_exchange_strong template with memory order types. Here is the syntax for the compare_exchange_strong with memory orders,

here is how we can use it,

Note that providing both compare_exchange_strong and compare_exchange_weak allows us to decide whether we want the library to handle spurious failures (using compare_exchange_strong) or if we want to handle it in ourr own code (using compare_exchange_weak). The compare_exchange_strong needs extra overhead to retry in the case of failure. For details, please see Load-link/store-conditional in Wikipedia.

Is there a simple example of strong compare and exchange in C++?

Here is a simple example about strong compare and exchange in modern C++.

Let’s assume we have a thread function myf1() that ensures value is changed after a strong compare and exchange. This is how we can do this,

Is there a full example about strong compare and exchange in C++?

Let’s assume we have a thread function myf1() that ensures value is changed after a strong compare and exchange. Let’s do the change after 1 second with myf2(). Here is the full example of strong compare and exchange in modern C++,

The output will be as follows.

As you see strong compare-and-exchange operation is an atomic operation that can be used in multi-threaded applications to achieve synchronization. For more information about this strong compare and exchange feature, see Strong Compare and Exchange Proposal document.

Learn Strong Compare and Exchange In Modern C++ C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond

C++C++11C++14C++17C++20Learn C++

What Is The Priority Queue (std::priority_queue) In Modern C++?