Site icon Learn C++

What Is Atomic (std::atomic) In Modern C++?

What Is Atomic stdatomic In Modern C++

In modern C++, the concurrency support library is designed to solve problems in multi-thread operations. This library includes built-in support for threads (std::thread), atomic operations (std::atomic), mutual exclusion (std::mutex), condition variables (std::condition_variable), and many other features of a modern C++ compiler. In this post, we explain what is std::atomic and how we can use atomic types efficiently in modern C++.

What is atomic (std::atomic) in modern C++?

In modern C++, the atomic library provides useful features for atomic operations allowing for lockless concurrent operations in C++. Every atomic operation is indivisible with regards to any other atomic operation that involves the same object.

C++11 adds atomic types and operations to the standard. Atomic types and operations provide a way of writing multi-threaded applications without using locks. In modern C++, the std::atomic<> template class is defined in <atomic> header and it can be used to wrap other types in order to do atomic operations on that type. When a thread writes to an atomic object another thread can read from it. Every instantiation and full specialization of the std::atomic template defines an atomic type.

Atomic types ensure any read or write operation synchronizes as part of multi-thread operations, (i.e. using these types in std::thread). They work well on private types (i.e. int, float, double, etc.) or any type that is trivially copyable types which means it has at least one eligible copy constructor, move constructor, copy assignment operator, or move assignment operator and has non-deleted trivial destructor.

Here is a simple syntax for the atomic declaration:

[crayon-6646c202719ef786042714/]

Here are syntaxes defined in <atomic> header since C++11:

[crayon-6646c202719f6843029411/]
[crayon-6646c202719f8420336551/]

Here are the syntaxes defined in the <memory> header since C++20:

[crayon-6646c202719f9239737938/]
[crayon-6646c202719fb705899622/]

std::atomic has many features to be used in atomic operations, i.e. load, store, operator=, wait, exchange, is_lock_free, etc. These will be explained in another post.

Is there a simple example of how to use std::atomic in modern C++?

Here is a simple std::atomic example:

[crayon-6646c202719fd809173328/]

Is there a full example of how to use std::atomic with lambda in modern C++?

Here is a full std::atomic example that uses std::thread, and std::vector:

[crayon-6646c202719fe233508640/]

Note that, every operation on these atomic types are called as atomic operation. For more information on this feature, see Atomic operations Proposal document.

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.

Exit mobile version