c++中的原子用于实现对象的线程安全的操作,避免数据竞争,每一个原子操作可以看作一个不可分割地整体。
本文章的代码库:https://gitee.com/gamestorm577/CppStd
atomic
atomic是一个类模板,每个atomic模板的实例化都定义了一个原子类型。对于一个原子对象,不同的线程对对象的写入和读取是安全的。atomic不能复制,也不能移动。
c++中为一些基本类型定义了atomic的别名,例如:
using atomic_bool   = atomic<bool>;
using atomic_char   = atomic<char>;
using atomic_schar  = atomic<signed char>;
using atomic_uchar  = atomic<unsigned char>;
using atomic_short  = atomic<short>;
using atomic_ushort = atomic<unsigned short>;
using atomic_int    = atomic<int>;
using atomic_uint   = atomic<unsigned int>;
using atomic_long   = atomic<long>;
using atomic_ulong  = atomic<unsigned long>;
using atomic_llong  = atomic<long long>;
using atomic_ullong = atomic<unsigned long long>;
成员函数
构造函数
默认构造,或者用一个值构造一个atomic对象。代码示例:
std::atomic<int> a_i(20);
std::cout << "a_i = " << a_i << std::endl;
输出结果:
a_i = 20赋值函数
将值原子地赋值给对象。代码示例:
std::atomic<int> a_i(20);
std::cout << "a_i = " << a_i << std::endl;
a_i = 25;
std::cout << "a_i = " << a_i << std::endl;
输出结果:
a_i = 20
a_i = 25is_lock_free
检查原子类型的原子操作是否是无锁的。代码示例:
struct A
{int a[100];
};struct B
{int x;int y;
};std::cout << std::boolalpha;
std::cout << "A is_lock_free: " << std::atomic<A>().is_lock_free() << std::endl;
std::cout << "B is_lock_free: " << std::atomic<B>().is_lock_free() << std::endl;
输出结果:
A is_lock_free: false
B is_lock_free: truestore
用指定值原子地替换当前的值。代码示例:
std::atomic<int> a_i(20);
std::cout << "a_i = " << a_i << std::endl;
a_i.store(35);
std::cout << "a_i = " << a_i << std::endl;
输出结果:
a_i = 20
a_i = 35load
原子地获取原子对象当前的值。代码示例:
std::atomic<int> a_i(25);
std::cout << "a_i = " << a_i.load() << std::endl;
输出结果:
a_i = 25operator T
原子地返回原子对象当前的值。
exchange
原子地替换对象的值,并返回对象先前持有地值。代码示例:
std::atomic<int> a_i(25);
int num1 = a_i.exchange(30);
int num2 = a_i.load();
std::cout << "num1 = " << num1 << std::endl;
std::cout << "num2 = " << num2 << std::endl;
输出结果:
num1 = 25
num2 = 30compare_exchange
原子地比较对象地值和Expected,如果是逐位相等的,那么用Desired替换对象的值,否则将对象的值赋给Expected:
bool compare_exchange_strong(TVal& Expected, const TVal Desired,const memory_order Order = memory_order_seq_cst);代码示例:
std::atomic<int> a_i1(25);
int Expected1 = 33;
a_i1.compare_exchange_strong(Expected1, 17);
std::cout << "ai_1 = " << a_i1 << std::endl;
std::cout << "Expected1 = " << Expected1 << std::endl;std::atomic<int> a_i2(25);
int Expected2 = 25;
a_i2.compare_exchange_strong(Expected1, 17);
std::cout << "ai_2 = " << a_i2 << std::endl;
std::cout << "Expected2 = " << Expected2 << std::endl;
输出结果:
ai_1 = 25
Expected1 = 25
ai_2 = 17
Expected2 = 25notify_one
唤醒一个原子对象在阻塞中的线程
notify_all
唤醒所有原子对象在阻塞中的线程
wait
比较原子对象的值和Expected,如果相等,则阻塞线程直到被唤醒。否则直接返回:
void wait(const TVal Expected, const memory_order Order = memory_order_seq_cst);代码示例:
std::atomic<int> a_i(25);
std::thread t = std::thread([&]() {a_i.wait(25);std::cout << "sub thread finish wait" << std::endl;
});std::cout << "main thread" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3));
a_i.store(17);
a_i.notify_one();t.join();
输出结果:
main thread
sub thread finish wait常量
is_always_lock_free
指示该类型是否始终免锁
特化成员函数
只有部分类型特化才有的成员函数
fetch_add
原子地加上给定值,并返回对象原来保有的值。
fetch_sub
原子地减去给定值,并返回对象原来保有的值。
fetch_and
原子地进行对象值和给定值的逐位与操作,并返回对象原来保有的值。
fetch_or
原子地进行对象值和给定值的逐位或操作,并返回对象原来保有的值。
fetch_xor
原子地进行对象值和给定值的逐位异或操作,并返回对象原来保有的值。
操作运算符
operator++、operator--令对象的值原子地加一或减一。
operator+=、operator-=、operator&=、operator|=、operator^=对对象值原子地做加、减、逐位与、逐位或、逐位异或操作。
atomic_ref
atomic_ref类模版对它引用的对象应用原子操作。在atomic_ref对象的生命周期中,atomic_ref把其引用的对象看作是一个原子对象。代码示例:
auto Func1 = [](std::vector<int>& arr)
{for (int& num : arr){++num;}
};auto Func2 = [](std::vector<int>& arr)
{for (int& num : arr){auto tmp = std::atomic_ref<int>(num);++tmp;}
};auto test_func = [](std::function<void(std::vector<int>&)> Func)
{std::vector<int> arr(100000, 0);{std::jthread t1(Func, std::ref(arr));std::jthread t2(Func, std::ref(arr));std::jthread t3(Func, std::ref(arr));std::jthread t4(Func, std::ref(arr));}int sum = std::accumulate(arr.begin(), arr.end(), 0);std::cout << "total sum = " << sum << std::endl;
};test_func(Func1);
test_func(Func2);
可能的输出结果:
total sum = 398665
total sum = 400000原子类型上的操作
c++还提供了原子类型上的函数接口,对应于原子对象的成员函数:
atomic_is_lock_free              is_lock_free    
atomic_store                     store   
atomic_load                      load
atomic_exchange                  exchange
atomic_compare_exchange_weak     compare_exchange_weak
atomic_compare_exchange_stong    compare_exchange_stong
atomic_fetch_add                 fetch_add
atomic_fetch_sub                 fetch_sub
atomic_fetch_and                 fetch_and
atomic_fetch_or                  fetch_or 
atomic_fetch_xor                 fetch_xor
atomic_wait                      wait
atomic_notify_one                notify_one
atomic_notify_all                notify_allatomic_flag
atomic_flag是一种原子布尔类型,和atomic<bool>不同的是,atomic_flag保证是无锁的。
成员函数
test
原子地返回flag的值。代码示例:
std::atomic_flag af;
std::cout << std::boolalpha;
std::cout << "flag: " << af.test() << std::endl;
输出结果:
flag: falsetest_and_set
原子地设置flag为true并返回其先前的值。代码示例:
std::atomic_flag af;
bool flag1 = af.test_and_set();
bool flag2 = af.test();
std::cout << std::boolalpha;
std::cout << "flag1: " << flag1 << std::endl;
std::cout << "flag2: " << flag2 << std::endl;
输出结果:
flag1: false
flag2: trueclear
原子地设置flag为false。代码示例:
std::atomic_flag af;
af.test_and_set();
bool flag1 = af.test();
af.clear();
bool flag2 = af.test();
std::cout << std::boolalpha;
std::cout << "flag1: " << flag1 << std::endl;
std::cout << "flag2: " << flag2 << std::endl;
输出结果:
flag1: true
flag2: falsenotify_one
唤醒一个原子对象在阻塞中的线程
notify_all
唤醒所有原子对象在阻塞中的线程
wait
比较原子对象的值和Expected,如果相等,则阻塞线程直到被唤醒。否则直接返回:
void wait(const bool Expected, const memory_order Order = memory_order_seq_cst);非成员函数
非成员函数对应于atomic_flag的成员函数:
atomic_flag_and_set    flag_and_set
atomic_clear           clear
atomic_test            test
atomic_wait            wait
atomic_notify_one      notify_one
atomic_notify_all      notify_all