public member function
<atomic>

std::atomic::operator=

set value (1)
T operator= (T val) noexcept;
T operator= (T val) volatile noexcept;
copy [deleted] (2)
atomic& operator= (const atomic&) = delete;
atomic& operator= (const atomic&) volatile = delete;
Assign contained value
Replaces the stored value by val.

This operation is atomic and uses sequential consistency (memory_order_seq_cst). To modify the value with a different memory ordering, see atomic::store.

atomic objects have no copy assignment defined, but note that they are implicitly convertible to T.

Parameters

val
Value to copy to the contained object.
T is atomic's template parameter (the type of the contained value).

Return value

val

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// atomic::operator=/operator T example:
#include <iostream>       // std::cout
#include <atomic>         // std::atomic
#include <thread>         // std::thread, std::this_thread::yield

std::atomic<int> foo = 0;

void set_foo(int x) {
  foo = x;
}

void print_foo() {
  while (foo==0) {             // wait while foo=0
    std::this_thread::yield();
  }
  std::cout << "foo: " << foo << '\n';
}

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


Output:
foo: 10

Data races

No data races (atomic operation). The operation uses sequential consistency (memory_order_seq_cst).

Exception safety

No-throw guarantee: never throws exceptions.

See also