[C ++] atomic atomic basic usage template function.

[Reserved]: https://owent.net/2012/611.html

 

The main function is as follows:

 函数名     |     描述     |

----- | ----- |
atomic_store | save data to a non-atomic atomic data structures |
atomic_load | read data in the atomic structure |
atomic_exchange | save data to a non-atomic atomic data structure, stored data back to the original |
atomic_fetch_add | data atomic structure do add operation |
atomic_fetch_sub / atomic_fetch_sub_explicit | data atomic structure do subtraction |
atomic_fetch_and | atomic structure data logic |
atomic_fetch_or | data logical atomic structure or |
atomic_fetch_xor | atomic structure of data in the logical exclusive oR

Memory operation just mentioned rules atomic operation time, memory operation rules are mainly std :: memory_order, which is an enumerated type, which contains rules plurality N

   值           |    定义规则      |

------- | ------ |
memory_order_relaxed | does not guarantee the order |
memory_order_consume | analogy producer - consumer consumer model reading operation (read only, no counter), ensure that the operation prior to depend on the data currently reading (for example, use the back of this data read) will not be in advance, but does not guarantee other sequential read operations. Most of compiling multithreaded programs, only compiler optimization process affects the environment. |
Memory_order_acquire | analogy producer - consumer model in consumer reading operation (read only, no counter), to ensure that all operations after this operation will not be ahead, same build environment for most of multithreaded programs compiler optimization process is affected. |
Memory_order_release | analogy producer - producer-consumer model to create an action (only one data operation), to ensure that this operation will not be delayed before. |
Memory_order_acq_rel | memory_order_release tag and contains memory_order_acquire |
memory_order_seq_cst | full access are executed sequentially, in a multi-core system can easily become a performance bottleneck |

In front of the function of the atomic operation, a default rule is std :: memory_order_seq_cst
addition, Atomic some type of marker and test operation, the operating system more similar atomic operation

  • std :: atomic_flag: Tag Type
  • atomic_flag_test_and_set: Try to occupy (atomic)
  • atomic_flag_clear: releasing (atomic operations)

    std::atomic_flag lock = ATOMIC_FLAG_INIT;
    
    void f(int n) { for(int cnt = 0; cnt < 100; ++cnt) { while(std::atomic_flag_test_and_set_explicit(&lock, std::memory_order_acquire)); std::cout << "线程 " << n << std::endl; std::atomic_flag_clear_explicit(&lock, std::memory_order_release); } } int main() { std::atomic_int a; a.store(100); a.fetch_add(105); int i = a.load(std::memory_order_consume); printf("i => %d\n", i); // 原子标记 std::vector<std::thread> v; for (int n = 0; n < 10; ++n) { v.emplace_back(f, n); } for (auto t = v.begin(); t != v.end(); ++ t) { t->join(); } return 0; }

Guess you like

Origin www.cnblogs.com/cy1993/p/11498768.html