C++ Atomic Library - load



Description

It atomically obtains the value of the atomic object.

Declaration

Following is the declaration for std::atomic::load.

T load( std::memory_order order = std::memory_order_seq_cst ) const;

C++11

T load( std::memory_order order = std::memory_order_seq_cst ) const volatile;

Parameters

order − It is used to enforce memory order constraints.

Return Value

It returns the current value of the atomic variable.

Exceptions

No-noexcept − this member function never throws exceptions.

Example

In below example for std::atomic::load.

#include <atomic>
#include <thread>

std::atomic<int> foo (0);

void set_foo(int x) {
   foo.store(x,std::memory_order_relaxed);
}

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

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