public member function
<atomic>

std::atomic::load

T load (memory_order sync = memory_order_seq_cst) const volatile noexcept;T load (memory_order sync = memory_order_seq_cst) const noexcept;
Read contained value
Returns the contained value.

The operation is atomic and follows the memory ordering specified by sync.

引數

sync
操作的同步模式。
This shall be one of these possible values of the enum type memory_order
memory order描述
memory_order_relaxedRelaxedNo synchronization of side effects.
memory_order_consumeConsumeSynchronizes the visible side effects on values carrying dependencies from the last release or sequentially consistent operation.
memory_order_acquireAcquireSynchronizes all visible side effects from the last release or sequentially consistent operation.
memory_order_seq_cstSequentially consistentSynchronizes all visible side effects with the other sequentially consistent operations, following a single total order.

返回值

The contained value.
Tatomic 的模板引數(包含值的型別)。

示例

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
27
// atomic::load/store example
#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,std::memory_order_relaxed);     // set value atomically
}

void print_foo() {
  int x;
  do {
    x = foo.load(std::memory_order_relaxed);  // 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


資料競爭

無資料競爭(原子操作)。記憶體順序由引數 sync 指定。

異常安全

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

另見