c++11 atomic 之 c-style接口使用

c++11 atomic 之 c-style接口使用

atomic的c-style接口 :提供相同语义但是不使用诸如:template reference 和member fucntion 等c++特性,整个atomic接口有一个C-style 对等品,成为C standard的一份补充,C-style接口一般适用于需要在C和C++之间保持兼容的代码身上 。

1、c-style变量

下表列出了类型对照:
18.12 若干已有确切名称的atomic_xx
更加详细详见
http://www.cplusplus.com/reference/atomic/

2、c-style 接口

Functions for atomic objects (C-style)
c-style describe
atomic_is_lock_free Is lock-free (function )
atomic_init Initialize atomic object (function )
atomic_store Modify contained value (function )
atomic_store_explicit Modify contained value (explicit memory order) (function )
atomic_load Read contained value (function )
atomic_load_explicit Read contained value (explicit memory order) (function )
atomic_exchange Read and modify contained value (function )
atomic_exchange_explicit Read and modify contained value (explicit memory order) (function )
atomic_compare_exchange_weak Compare and exchange contained value (weak) (function )
atomic_compare_exchange_weak_explicit Compare and exchange contained value (weak, explicit) (function )
atomic_compare_exchange_strong Compare and exchange contained value (strong) (function )
atomic_compare_exchange_strong_explicit Compare and exchange contained value (strong, explicit) (function )
atomic_fetch_add Add to contained value (function )
atomic_fetch_add_explicit Add to contained value (explicit memory order) (function )
atomic_fetch_sub Subtract from contained value (function )
atomic_fetch_sub_explicit Subtract from contained value (explicit memory order) (function )
atomic_fetch_and Apply bitwise AND to contained value (function )
atomic_fetch_and_explicit Apply bitwise AND to contained value (explicit memory order) (function )
atomic_fetch_or Apply bitwise OR to contained value (function )
atomic_fetch_or_explicit Apply bitwise OR to contained value (explicit memory order) (function )
atomic_fetch_xor Apply bitwise XOR to contained value (function )
atomic_fetch_xor_explicit Apply bitwise XOR to contained value (explicit memory order) (function )
Functions for atomic flags (C-style)
c-style describe
atomic_flag_test_and_set Test and set atomic flag (function )
atomic_flag_test_and_set_explicit Test and set atomic flag (explicit memory order) (function )
atomic_flag_clear Clear atomic flag (function )
atomic_flag_clear_explicit clear atomic flag (explicit memory order) (function )

3、例子

#include <atomic>
#include <iostream>

int main(void)
{
    std::atomic_bool  atomic_bool_test1;
    std::atomic_init(&atomic_bool_test1,false);
    std::cout << "init atomic_bool_test1 "<<atomic_bool_test1<<std::endl;
    std::atomic_store(&atomic_bool_test1,true);
    std::cout << "init atomic_bool_test1 "<<atomic_bool_test1<<std::endl;
    std::cout << "init atomic_bool_test1 "<<atomic_load(&atomic_bool_test1)<<std::endl;
}
4 c11中原子操作相关(未完待续。。。。)

c11中同样也支持了原子操作,相关内容单独进行解析;
可以推荐几篇不错的博文
C11标准库中的atomic原子操作

https://en.cppreference.com/w/c/atomic

5 参考

http://www.cplusplus.com/reference/atomic/
《c++标准库 第二版》

发布了67 篇原创文章 · 获赞 15 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/wanxuexiang/article/details/104287398