Home > Backend Development > C++ > body text

What is an Atomic Object in std::atomic<>?

Linda Hamilton
Release: 2024-11-10 16:33:02
Original
515 people have browsed it

What is an Atomic Object in  std::atomic<>? 
? " />

What is Atomic Object in std::atomic<>?

The std::atomic<> template provides a type that can be concurrently operated on by multiple threads without raising undefined behavior.

Atomicity of std::atomic<>

Each instantiation of std::atomic<> represents an atomic type that allows for simultaneous operations by different threads on its instances. Unlike regular C objects, atomic objects ensure exclusive access to their data, preventing data races.

Atomic Operations

Operations performed on atomic objects are atomic in nature. This means that:

  • Changes to an atomic object's state are guaranteed to be visible to all threads.
  • Accesses to atomic objects may establish inter-thread synchronization and order non-atomic memory accesses.

Memory Order and std::atomic<>

std::atomic<> provides precise control over synchronization and memory order. It supports various memory order options, including:

  • std::memory_order_relaxed: No synchronization or ordering guarantees.
  • std::memory_order_release: Ensures that writes are visible to other threads acquiring the same atomic variable.
  • std::memory_order_acquire: Ensures that loads are visible after a release operation on the same atomic variable in other threads.
  • std::memory_order_seq_cst: Provides sequential consistency (total global ordering) between all atomic operations.

Example: Arithmetic Operations

While individual arithmetic operators like = and are atomic operations, decomposing them into separate load, add, and store operations may not be atomic. For example:

a = a + 12;
Copy after login

This operation will involve the following steps:

  1. Load a's value.
  2. Add 12 to it.
  3. Store the result back to a.

Steps 2 and 3 are not atomic, so there is a potential race condition where another thread could modify a between the add and the store.

Alternatives to Basic Arithmetic Operations

For typical use cases involving arithmetic operations, it's recommended to use the equivalent overloaded operators defined in std::atomic<>. These operators perform the entire operation atomically, ensuring sequential consistency.

Additional Resources on Atomicity and Memory Model

  • C atomics and memory ordering
  • Comparison: Lockless programming with atomics in C 11 vs. mutex and RW-locks
  • C 11 introduced a standardized memory model. What does it mean? And how is it going to affect C programming?
  • Concurrency in C 11

The above is the detailed content of What is an Atomic Object in std::atomic<>?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template