std::atomic<T>

suggest change

std::atomic<T> template allows thread safe writing, reading and incrementing numeric values.

Reading and writing memory from different threads leads to data races and unpredictable results.

std::atomic adds necessary barriers to make the operations thread safe.

Example of using std::atomic_int:

#include <iostream>       // std::cout
#include <atomic>         // std::atomic, std::memory_order_relaxed
#include <thread>         // std::thread

std::atomic_int foo (0);

void set_foo(int x) {
  foo.store(x);     // set value atomically
}

void print_foo() {
  int x;
  do {
    x = foo.load();  // get value atomically
  } while (x==0);
  std::cout << "foo: " << x << '\n';
}

int main ()
{
  std::thread first (print_foo);
  std::thread second (set_foo,10);
  first.join();
  second.join();
  return 0;
}
foo: 10

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


std::atomic:

Table Of Contents
8 Arrays
11 Loops
22 std::atomic
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype