Chapter 5 atomic C ++ memory model and the type of operation

5.1 memory model basis

5.1.1 objects and memory locations

C ++ program All data is composed of objects, but some of them can not be derived class, these data in memory has a strict placement order, so as to ensure the correctness of the development, in a composite structure, we can get:

1. Each variable is an object, including members of other objects

2. Each object occupies at least one memory location

3. int char or such basic types of variables exactly one memory location, regardless of the size of a portion, or even if they are adjacent array

4. adjacent bit field is part of the same memory location

// For 3,4 points, I feel strange

5.1.2 objects, as well as concurrent memory location

For concurrent access to objects, if multiple threads access the object is not in the same position, it is not necessary to consider the competition caused by the data is written, read integrity problems only occur when the data should be considered competition to write data, read integrity between the take.

5.1.3 modify the order

C ++ programs each object, modify the order has a settlement, all threads in the system must be agreed this order

Atomic type and the 5.2 C ++

Atomic operation is an indivisible operation, which has a characteristic, either done, or not done, the other threads when accessed, can not access to the intermediate state such processes.

5.2.1 Standard atom type

There are many types of standard atoms

Operations on 5.2.2 std :: atomic_flag

For std :: atomic_flag initialization can only use ATOMIC_FLAG_INIT.

5.2.3 std :: atomic <bool> operation

5.2.4 std :: atomic operations <T *> on: pointer arithmetic operators

5.2.5 Standard Operation atoms integer

5.2.6 std :: atomic <> template class primary

5.2.7 Function atomic operation consisting of

For the atomic operation is not the only member functions, of course, there are also non-member functions, for most non-member function simply add the prefix atomic_ original function basis. Where have the opportunity to specify the memory sequence tags, they have two variants: one is no label, a suffix is ​​added _explict and additional parameters as a memory sequence label.

 

Atomic type of operations available
operating atomic_flag atomic<bool> atomic<T*> atomic<integral-type> atomic<othre-type>
test_and_set        
clear        
is_lock_free  
load  
store  
exchange  
compare_exchange_weak  
compare_exchange_strong          
fetch_add, +=      
fetch_sub, -=      
fetch_or, |=        
fetch_and, &=        
fetch_xor, ^=        
++, --      

Guess you like

Origin www.cnblogs.com/hebust-fengyu/p/12087824.html