版权声明:本文为博主原创文章,转载请注明出处。 https://cloud.tencent.com/developer/article/1433322
#include <iostream>
#include <boost/atomic.hpp>
using namespace std;
using namespace boost;
int main()
{
atomic<int> a(10);
assert(a == 10);
atomic<long> l;
cout << l << endl;
atomic<bool> b(false);
assert(!b.load());
b.store(true);
assert(b);//Òþʽload
atomic<int> n(100);
assert(n.exchange(200) == 100);
assert(n == 200);
atomic<long> ll(100);
long v = 100;
bool bres = false;
bres = ll.compare_exchange_weak(v, 313);//cmpare and swap,返回true,ll==313,v不变
cout << "bres = " << bres << endl;
cout << "ll = " << ll.load() << endl;
cout << "v = " << v << endl;
v = 200;
bres = ll.compare_exchange_strong(v, 99);//ll!=v,返回false,v==ll.
cout << "bres = " << bres << endl;
cout << "ll = " << ll.load() << endl;
cout << "v = " << v << endl;
system("pause");
}