public member function
<atomic>

std::atomic::operator=

設定值 (1)
T operator= (T val) noexcept;T operator= (T val) volatile noexcept;
複製 [已刪除] (2)
atomic& operator= (const atomic&) = delete;atomic& operator= (const atomic&) volatile = delete;
賦值包含的值
儲存的值替換為 val

此操作是原子的,並且使用順序一致性memory_order_seq_cst)。要使用不同的記憶體順序來修改值,請參見 atomic::store

atomic 物件沒有定義複製賦值,但請注意它們可以隱式轉換為 T

引數

val
要複製到包含物件的值。
Tatomic 的模板引數(包含值的型別)。

返回值

val

示例

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;
}

輸出
foo: 10


資料競爭

無資料競爭(原子操作)。該操作使用*順序一致性*(memory_order_seq_cst)。

異常安全

無異常保證: 絕不丟擲異常。

另見