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) {;     // 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);
  return 0;
foo: 10

Feedback about page:

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

Table Of Contents