Reading notes "C ++ Programming concurrent real" (5) - atomic memory model and the type of operation

Memory models:
    C ++ memory location layout, see structure struct / class members of the class / global static variable in memory layout.
    Multiple threads modify the value of a location, or should be modified to read certain order, otherwise the data competition or undefined behavior may occur.

Atomic operation:
    Similar to a transaction operation, are read or modify the object values ​​atoms, where a fail value will not be partially modified or read.
    Standard Atomic atom type :: STD <> template type, a specialized version of the plurality of types of built-in types, such as atomic_bool, atomic_int, atomic_size_t the like, in addition to support user
    Custom variant types of atoms, the atom type operation section interface provides a variety of optional parameter sequence memory, the default memory_order_seq_cst;
    
    std :: atomic_flag basic types of atoms, represent only boolean flag, there are two states: setting test_and_set, Clear Clear; in addition to initialize the initialization need ATOMIC_FLAG_INIT
    Examples of types of atoms. Spin lock can be achieved, but because of limitation, not to use this general type, but the use of Atomic :: STD < BOOL > type (std :: atomic_bool).
    
    :: Atomic std < bool > : provide write store, read-modify-write operation exchange, query (load) operating load or implicitly converted to bool type, in addition to compare_exchange_strong,
    compare_exchange_weak comparison determination swap function.
    Atomic :: STD <T *> :: Atomic provide and STD < BOOL > similar external interface also supports fetch_add, fetch_sub, ++ / - operation.
    
    Other types such as the built-in atomic atomic_int, there may be additional user interface, such as fetch_add, fetch_sub, fetch_and, fetch_xor compound assignment, etc., the front / postfix increase / decrease the like.
    
    User-defined types of atomic types: bitwise assignment to be fulfilled, according to the characteristics of the bit comparison, i.e. custom type must be trivial, and there is no virtual function member or a virtual functions.
    (std :: Atomic < float / Double > is not recommended, similar Atomic :: std <std :: the Vector < int >> also can not be used).
    
    Other free function of atomic operations: the form: std :: atomic_load, std :: atomic_is_lock_free, std :: atomic_compare_exchange_weak,
    std::atomic_flag_clear_explicit等。
    
    Not only atomic operations to avoid data races, but also force the sequence of operations between threads.
    
Atomic memory sequence (std :: memory_order):
    Compiler optimization, cpu instruction pipeline may generate instructions out of order, in order to optimize the efficiency;
    C ++ available in three models: the order of the same order, obtain - the release order, a loose order, the performance of six kinds of memory order, the order has a different memory may be different costs on different CPU architectures, such as;
    memory_order_relaxed,memory_order_consume,memory_order_acquire,memory_order_release,memory_order_acq_rel,memory_order_seq_cs。
    
    Sequentially consistent order:
        Default order memory_order_seq_cs, as the most visually sort, the behavior consistent with the order of the single thread execution order, before a thread when another thread operation,
        The order must be visible to other threads, and requires global synchronization between threads, in some multi-processor systems require intensive and time consuming communication between the processor, synchronizing a certain cost.
    
    memory_order_relaxed:
        Atomic operation according to a single thread or the happens - before relationship, but the relationship between different execution threads is arbitrary. It requires the same thread can not be rearranged to access individual atoms, once
        Given thread has been handled in the yard to see the custom, after reading this thread will not be able to get the value of the variable earlier. In the case where no additional synchronization, the order of each modification is to use variable
        memory_order_relaxed。
    
    Memory Barrier: std :: atomic_thread_fence, as a global operation, can affect the operation of other atoms in the order of execution of the thread in the barrier, to limit the hardware or by the compiler reordered.

 

Guess you like

Origin www.cnblogs.com/haomiao/p/11647403.html