Namespaces
Variants
Views
Actions

std::atomic_thread_fence

From cppreference.com
< cpp‎ | atomic
Revision as of 16:05, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <atomic>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
extern "C" void atomic_thread_fence( std::memory_order order );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end

Establishes memory synchronization ordering of non-atomic and relaxed atomic accesses, as instructed by order, without an associated atomic operation. For example, all non-atomic and relaxed atomic stores that happen before a std::memory_order_release fence in thread A will be synchronized with non-atomic and relaxed atomic loads from the same locations made in thread B after an std::memory_order_acquire fence.

Contents

Parameters

order - the memory ordering executed by this fence

Return value

(none)

Exceptions

noexcept specification:  
noexcept
  

Examples

Scan an array of mailboxes, and process only the ones intended for us, without unnecessary synchronization.

const int num_mailboxes = 32;
std::atomic<int> mailbox[num_mailboxes];
 
// The writer threads update non-atomic shared data and then update mailbox[i] as follows
 std::atomic_store_explicit(&mailbox[i], std::memory_order_release);
 
// Reader thread needs to check all mailbox[i], but only needs to sync with one
 for (int i = 0; i < num_mailboxes; ++i) {
    if (std::atomic_load_explicit(&mailbox[i],  std::memory_order_relaxed) == my_id) {
        std::atomic_thread_fence(std::memory_order_acquire); // synchronize with just one writer
        do_work(i); // guaranteed to observe everything done in the writer thread before
                    // the atomic_store_explicit()
    }
 }


See also

Template:cpp/atomic/dcl list memory orderTemplate:cpp/atomic/dcl list atomic signal fence
C documentation for atomic_thread_fence