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-6763972f08413270107456/]

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

[crayon-6763972f0841e870355635/]
[crayon-6763972f08421156112631/]

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

[crayon-6763972f08422014277731/]
[crayon-6763972f08427242247750/]

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-6763972f08429788326576/]

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-6763972f0842b441030429/]

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